0

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  = $row['name']; 

    echo "<ul>\n"; 
    echo "<li>" . "<a  href=\"player-info.php?id=\">"   .$name . " </a></li>\n"; 
    echo "</ul>"; 
}

3 Answers 3

3

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>"; 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Check out mysql_num_rows

Comments

1

mysql_num_rows

$numberOfResults = mysql_num_rows($q);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.