对数组中的每个结果运行php查询

时间:2013-05-11 12:53:07

标签: php mysql

我有两个数据库。数据库1具有按标题,id,用于(按位置)的人员以及文档在数据库中的位置存储的上载文档的列表。每次读取书籍时,第二个数据库都会存储详细信息。这将存储id(取自第一个数据库)和登录的用户名。

我有两个问题。第一个在第二个数据库中查找,并将所有具有当前用户编号的ID存储为数组。 (回声用于测试。)第二个查找第一个数据库并列出所有未读取的文档,但查找的项目与找到的详细信息不同。但是,我坚持让第二个查询循环遍历第一个查询找到的每个id,因此它只搜索第一个数字。

这是我的代码:

<?php
$connection = mysql_connect("host","database","password") or die("Couldn't make connection."); 
$db = mysql_select_db("database", $connection) or die("Couldn't select database."); 
$sql = "SELECT *  FROM `READ` WHERE `name` = '$user'";                      
$sql_result = mysql_query($sql,$connection) or die("Couldn't execute query 1."); 

while($row1 = mysql_fetch_array($sql_result))                           
{
   echo "<br>".$row1['item'];
   $ident = $row1["item"];                                              
}

mysqli_close($connection);                                          
?>

<?php
$connection = mysql_connect("host","database","password") or die("Couldn't make connection."); 
    $db = mysql_select_db("database", $connection)  or die("Couldn't select database."); 
$query ="SELECT * FROM UPLOAD WHERE id NOT LIKE '$ident' AND faoGrade LIKE '%$gr%'";       
   //$ident only searching the first number stored
$query_result = mysql_query($query,$connection) or die("Couldn't execute query 2.");  
while($row2 = mysql_fetch_array($query_result))                             
{
   echo "<br>".$row2["title"];      
}

mysqli_close($connection);                                                                      
?>

我肯定会超越目前的知识,认为我需要for i=for each id foundwhile,但我不确定如何让它发挥作用。 $gr$user是页面顶部的会话变量。我已经更新到mysqli,但我还不熟悉它。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。

    <?php
        $connection1 = mysql_connect("host","username","password") or die("Couldn't make connection."); 
        $db = mysql_select_db("database", $connection1) or die("Couldn't select database."); 

        $connection2 = mysql_connect("host","username","password") or die("Couldn't make connection."); 
        $db = mysql_select_db("database", $connection2)  or die("Couldn't select database."); 

        $sql = "SELECT *  FROM `READ` WHERE `name` = '$user'";                      
        $sql_result = mysql_query($sql,$connection1) or die("Couldn't execute query 1."); 

        while($row1 = mysql_fetch_array($sql_result))                           
        {
           echo "<br>".$row1['item'];
           $ident = $row1["item"];

           //$gr is not assigned any value..plz check it
           $query ="SELECT * FROM UPLOAD WHERE id NOT LIKE '$ident' AND faoGrade LIKE '%$gr%'";       
           //$ident only searching the first number stored
           $query_result = mysql_query($query,$connection2) or die("Couldn't execute query 2.");  
           while($row2 = mysql_fetch_array($query_result))                             
           {
              echo "<br>".$row2["title"];      
           }
        }

        mysql_close($connection1);
        mysql_close($connection2);
?>

希望它能帮助您管理多个数据库连接。

注意:不推荐使用mysql_*扩展名。尝试使用mysqli_*PDO扩展名。