help with php search
How would I do let the user know if there wasnt any rows found? And if it finds (for example) 26 rows I want it to print that it found 26 hits (Your search for "hey" gave 26 results)
$name = mysql_real_escape_string($_POST['player_name']);
$q = mysql_query("SELECT * FROM players WHERE name LIKE '%$name%'");
while ($row = mysql_fetch_array($q)) {
$name = $ro开发者_C百科w['name'];
echo "<ul>\n";
echo "<li>" . "<a href=\"player-info.php?id=\">" .$name . " </a></li>\n";
echo "</ul>";
}
Use mysql_num_rows
:
$rowCount = mysql_num_rows($q);
if ($rowCount === 0)
{
echo 'Found nothing!';
}
else
{
echo "Found $rowCount rows!";
while ($row = mysql_fetch_array($q))
{
$name = $row['name'];
echo "<ul>\n";
echo "<li>" . "<a href=\"player-info.php?id=\">" .$name . " </a></li>\n";
echo "</ul>";
}
}
Check out mysql_num_rows
mysql_num_rows
$numberOfResults = mysql_num_rows($q);
精彩评论