How to solve PHP error : Warning: mysql_num_rows() expects parameter 1 to be resource, boolean on line 54

Hi,
I’m trying to create a basic Search Engine for practice. The code is as follows:

<?php  
                                mysql_connect("localhost", "username", "password");
                                mysql_select_db("timings");
                                $terms = explode(" ", $q);
                                $query = "SELECT * FROM timings WHERE ";

                                foreach ($terms as $key) {
                                    $i = 0;
                                    $i++;
                                    if ($i == 1) {
                                        $query .= "Last_Stop LIKE '$key' ";
                                    } else {
                                        $query .= "OR Last_Stop LIKE '$key' ";
                                    }
                                }

                                $query = mysql_query($query);
                                $rows = mysql_num_rows($query);

                                if($rows > 0) {
                                    while ($wor = mysql_fetch_assoc($query)) {
                                        $br_no = $wor['Bus_Route_No'];
                                        $s_stop = $wor['Start_Stop'];
                                        $lstop = $wor['Last_Stop'];

                                        print($br_no);
                                        echo($s_stop);
                                        echo($lstop);
                                    }
                                } else {
                                    echo("No Results Found");
                                }
                            ?>

When I execute this script it is giving the following warning.

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given on line 54.

Use a different variable name instead of overriding it.
$result = mysql_query($query);
$rows = mysql_num_rows($result);

Also avoid mysql. Instead use mysqli or pdo.

1 Like

Where is $q populated?

Try creating the following function and step through the program and show intermediate results.

<?php

function fred($value)
{
echo '<pre>';
  var_dump($value);
echo'</pre>';
}

fred($q); die;

...
...
fred($query); die:

You execute the query, but don’t check whether it was worked properly.

$query = mysql_query($query);
$rows = mysql_num_rows($query);

So my guess is that the query didn’t work (as @John_Betong said, where is $q coming from?) so it’s returned false for the value of $query, but then you haven’t checked that, and just tried to get the number of rows. You need to add error correction, but you also need to drop the old mysql() calls and switch to something that is still part of the language, as @nimasdj said above.

I don’t think this bit will do what you intend, either:

foreach ($terms as $key) {
  $i = 0;
  $i++;
  if ($i == 1) {
    $query .= "Last_Stop LIKE '$key' ";
    } else {
    $query .= "OR Last_Stop LIKE '$key' ";
  }
}

It seems to me that you need to move the line where you initialise $i to zero to be before the foreach() loop. At the moment, the value will always be 1, because you zero it every time, so you’ll have a syntax error in the query, which is why it doesn’t run, which leads to your error message.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.