I am trying to use a result from one query as input for another query but I am stumped. I thought about using JOIN but I think in this case the two queries need to me run separately. Essentially I have a list of articles in my database. As I loop through the list or articles that I obtained from my first query I want to search a second table to find out the number of votes that each article has.
This is the code:
<?php
$sql=mysql_query("SELECT * FROM discussion_links WHERE link_side = 'Michigan'");
while($row = mysql_fetch_array($sql)) {
?>
<div class="top-links-wrapper">
<div>
<div class="link-info">
<a class="link-title" href="http://<?php echo $row['link_url'] ?>">
<?php echo $row['link_title'] . "</a>"; ?>
<p class="link-source"><?php echo $row['link_source'] . "</p>" ?>
</div>
<div class="link-vote-wrapper">
<span class="link-votes">
<?php
$sql2=mysql_query("SELECT * FROM link_votes WHERE link_id = " . $row['link_id'] .")";
$num_rows = mysql_num_rows($sql2);
echo "$num_rows";
?>
</span>
</div>
</div>
</div>
Thanks
mysql_*functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information.