php - Loop through database query -
i show results of 1 database table use of variable fetched database table this:
mysql_select_db($database_connection, $connection); $query_recordset_bids = "select * bids 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 * jobs 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 want output showed in following table:
<?php if($totalrows_recordset_jobs == 0) echo "you have never submitted 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 { ?> <tr> <td height="22" bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['userid']; ?></td> <td bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['destination']; ?></td> <td bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['cargo']; ?></td> <td bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['due_date']; ?></td> <td bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['bids']; ?> kr.</td> <td bgcolor="#798890" scope="col"> <?php echo $row_recordset_jobs['status']; ?></td> </tr> <?php } while ($row_recordset_jobs = mysql_fetch_assoc($recordset_jobs)); ?> </table> <?php } ?>
but there 1 row shown in table though there 2 or more results match select query.
so how loop through first database table multiple matching variables (jobid) can use select statement second database table, should show multiple results?
i suggest learn joins. :)
you can use join on different databases aslong both databases available same connection/credentials.
i not sure if got right have 2 databases tables.
if have 1 database simple:
$query = 'select * bids b left join jobs j on b.jobid = j.userid b.bidder = "$username"';
incase have 2 databases use , insert names of 2 databases <namedb1> , <namedb2>.
however, note not smartest thing because cannot use indizes, transactions, constraints or table-locks on different databases. (as mentioned jay blanchard in comments)
$query = 'select * <namedb1>.bids b left join <namedb2>.jobs j on b.jobid = j.userid b.bidder = "$username"';
Comments
Post a Comment