Maggorra
Maggorra

Reputation: 3

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

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

Upvotes: 0

Views: 66

Answers (3)

Will Vousden
Will Vousden

Reputation: 33408

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>"; 
    }
}

Upvotes: 3

Simon
Simon

Reputation: 37998

mysql_num_rows

$numberOfResults = mysql_num_rows($q);

Upvotes: 1

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91983

Check out mysql_num_rows

Upvotes: 1

Related Questions