View Full Version : MySQL Is Saying There Are No Rows


Joseph Witchard
08-21-2009, 08:40 PM
$query1 = "SELECT COUNT(*) FROM posts WHERE post_body LIKE '%$search_term%'";

if ($result1 = $conn->query($query1))
{

// make sure something was returned

$stored = $result1->store_result;

$num_rows = $stored->num_rows;

if ($num_rows <= 0)
{

echo "<p class='warning'>Your search didn't match anything in our database.</p>";

}


So, I'm building a search form for my site. The problem is the form always tells me nothing is returned when I search for something that I know is in the database. What I find odd, though, is if I remove the code that checks to make sure something is returned, the same test search fetches everything from the database perfectly. What gives?

iTom
08-21-2009, 09:43 PM
So when you do:

$num_rows = $stored->num_rows;

does this access the number of rows returned or the field 'num_rows'?

Joseph Witchard
08-21-2009, 09:53 PM
Number of rows returned, I think. Here:
http://www.php.net/manual/en/mysqli.store-result.php (must do this first)
http://www.php.net/manual/en/mysqli-result.num-rows.php

iTom
08-23-2009, 04:14 AM
$result1->store_result is being used as a variable. It needs to be used as a function: $result1->store_result() (methinks).

iGeek
08-23-2009, 09:06 AM
Whatever class you're using to access the database is very inefficient. Tom is correct; $result1->store_result is accessing the property "store_result," not the method.

Joseph Witchard
08-24-2009, 12:55 AM
But when I use it that way, I get the following error:

Fatal error: Call to undefined method mysqli_result::store_result()