简体   繁体   中英

Loop through database query

I would like to show the results of one database table by the use of a variable fetched from another database table like this:

mysql_select_db($database_Connection, $Connection);
$query_Recordset_bids = "SELECT * FROM bids WHERE bidder = '$userName'";
$Recordset_bids = mysql_query($query_Recordset_bids, $Connection) or die(mysql_error());
while ($row_Recordset_bids = mysql_fetch_array($Recordset_bids)) {
$totalRows_Recordset_bids = mysql_num_rows($Recordset_bids);

mysql_select_db($database_Connection, $Connection);
$query_Recordset_jobs = "SELECT * FROM jobs WHERE userID = '".$row_Recordset_bids['jobID']."'";
$Recordset_jobs = mysql_query($query_Recordset_jobs, $Connection) or die(mysql_error());
$row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs);
$totalRows_Recordset_jobs = mysql_num_rows($Recordset_jobs);
}

And then I want the output showed in the following table:

<?php if($totalRows_Recordset_jobs == 0)
echo "You have never submitted a job offer!";
else {   
?>
<table width="440" border="0" cellpadding="1" cellspacing="1" id="tablejobs">
<tr>
    <th width="40" bgcolor="#779BDC" scope="col">ID</th>
    <th width="90" bgcolor="#779BDC" scope="col">Destination</th>
    <th width="85" bgcolor="#779BDC" scope="col">Cargo</th>
    <th width="85" bgcolor="#779BDC" scope="col">Due Date</th>
    <th width="75" bgcolor="#779BDC" scope="col">Bid</th>
    <th width="65" bgcolor="#779BDC" scope="col">Status</th>
</tr>
<?php do { ?>
  <tr>
    <td height="22" bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['userID']; ?></td>
    <td bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['destination']; ?></td>
    <td bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['cargo']; ?></td>
    <td bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['due_date']; ?></td>
    <td bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['bids']; ?> kr.</td>
    <td bgcolor="#798890" scope="col">&nbsp;<?php echo $row_Recordset_jobs['status']; ?></td>
  </tr>
  <?php } while ($row_Recordset_jobs = mysql_fetch_assoc($Recordset_jobs)); ?>
 </table>
<?php
}
?>

But there is only one row shown in the table even though there are 2 or more results that match the select query.

So how do I loop through the first database table to get multiple matching variables (jobID) that I can use to my select statement to the second database table, which should show multiple results?

I suggest you to simply learn about joins. :)

You can also use a join on different databases aslong as both databases are available with the same connection/credentials.

I am not sure if I got you right that you have two databases for your tables.

If you have only one database its simple:

$query = 'SELECT * 
          FROM bids b 
          LEFT JOIN jobs j ON b.jobID = j.UserID 
          WHERE b.bidder = "$userName"';

Incase you have two databases use this and insert names of your two databases for <namedb1> and <namedb2>.

However, note that this is not the smartest thing to do because you cannot use any indizes, transactions, constraints or table-locks over different databases. (As mentioned by Jay Blanchard in the comments)

$query = 'SELECT * 
          FROM <namedb1>.bids b 
          LEFT JOIN <namedb2>.jobs j ON b.jobID = j.UserID 
          WHERE b.bidder = "$userName"';

http://dev.mysql.com/doc/refman/5.1/en/join.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM