1

I am trying to perform an additional query as you can see from the below piece of code.

I am having issues performing the second query. It seems like the code is not recognizing the second query. Can someone please advice?

Thanks in advance.

$query  = "SELECT * FROM tst WHERE status ='active' order by created DESC LIMIT 9";
$result = mysql_query($query);
if (!$result) die  ("database access failed : "  .   mysql_error());

while($row = mysql_fetch_array($result))
{
  echo"<ul class='gallery'><li>";
  echo "<td>" . $row['adid'] . "</td>";
  $subquery = ("SELECT * FROM match_item_image where adid = '.$row['adid'].'  LIMIT 1 ");
  $results = mysql_query($subquery) or die ("Error in query: $subquery " . mysql_error());
  $rows = mysql_fetch_array($results);
  $rows = mysql_num_rows($results);

  echo   '  <a href="viewad.php?adid='.$row['adid'].'&subcat='.$row['subcat'].'">  <img src= "upload/'.$rows['image'].'"height="175" width="200"border="0"  /> </a> ';
  echo "<h2><a href=''>".$row['state'] . "</a></h2>";
  echo   "<h2><a href=''>".$row['loc'] . "</a></h2>";
  echo"
   </li>
 </ul>";
}
inhan
  • 7,394
  • 2
  • 24
  • 35
  • This is too much to edit, with the markup breaking code all over. Please paste in the code with its original indentation and linebreaks, then highlight it and ctl-k to format as a code block. – Michael Berkowski Nov 13 '12 at 02:30
  • Just a tip out of the topic, but for boolean value columns, use {0,1} instead of string {"active", "deactive"}. – BernaMariano Nov 13 '12 at 02:42

1 Answers1

4

the query is not working because you have included the $row['adid'] as a string and not as a value in the query, concactenate it like this

"SELECT * FROM match_item_image where adid = '" . $row['adid'] . "'  LIMIT 1 "

and your query is now vulnerable with SQL Injection, please read the article below

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    Thanks John, I got the code to work. Thanks for your input. I will make sure I fix my code to make sure it is not vulnerable to SQL injection. Thanks buddy. – andi flores Nov 13 '12 at 02:49