0

I have two databases. Database one has a list of uploaded documents stored by title, id, who's it for (by position) and location of the document in the database. The second database stores details every time the book is read. This stores the id (taken from the first database) and the logged in username.

I have two queries. The first looks in the second database and stores all the ids with the current user number as an array. (The echo is for testing.) The second looks in the first database and lists all the documents not read but looks for items not like the details found. However, I am stuck with making the second query loop through each id that was found by the first query, so it only searches the first number.

This is my code:

<?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);                                                                      
?>

I am definitely going past my current knowledge and think I need a for i=, for each id found or a while, but I'm not sure how to get it to work. the $gr and $user are session variables at the top of the page. I have updated to mysqli, but I'm not to familiar with it yet.

4

1 回答 1

0

Try below code.

    <?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);
?>

Hope it will help you for managing multiple database connections.

Note : mysql_* extensions are deprecated though. Try to use mysqli_* or PDO extensions.

于 2013-05-11T16:47:26.300 回答