The query below will be used a search script. For some reason it won’t return all results where either condition is true. What am i doing wrong?
$sql = "SELECT name, id_code from codes WHERE name LIKE '%$q%' OR id_code
LIKE '%$q%'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("<div id=norequests>No results for <strong>$q</strong></div>");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$name = htmlspecialchars($row['name']);
$code = htmlspecialchars($row['id_code']);
}
print("$code: $name<br /> <br />");
}
}
else{
echo '<div id="error">No results for $q.</div>';
}
You are printing outside of
while. Which means, no matter how many results you have, only the one will be printed.Either print inside the loop
or collect the variables in an array while looping and use them after the loop as you like