-1

I am trying to do the following.

SELECT from database and echo, If there are less than 3 results, I want to subtract the amount of results from 3 and use an alternative SELECT string, The code works and does what I expect of it except for the error in the logs.

PHP Warning: mysql_num_rows() expects parameter 1 to be resource, null given in

This error seems to be because the result is empty, How can I check how many rows in a result without getting an error when the table has no results.

$sqls="SELECT  * FROM xxx ORDER by xxx_id ASC LIMIT 0,3";
    $objRs = mysql_query($sqls);

The database works, the SQL query works, the row below this is the one giving me the error when I have a NULL result. Is there another way to check the number of rows without getting the server pissed at me

    $count = mysql_num_rows($objRS);

        If (!empty($count)) { 
                    $pnb = 3;
            }   else {
                while($rows = mysql_fetch_array($objRs, MYSQL_ASSOC)) {
Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
  • You have different case of the last letter of $objRs when you use and initialize it. – wazelin Jun 24 '16 at 12:34
  • please use mysqli or pdo - mysql is deprecated. example use of mysqli `(oop) $res = $conn->query($sql)` or `(procedural) $res= mysqli_query($conn, $sql)` – Matthew Carpenter Jun 24 '16 at 12:35
  • Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – codedge Jun 24 '16 at 12:35

1 Answers1

4

The code "works" by coincidence alone. You have a typo:

$objRs

is not the same thing as

$objRS

Variable names are case-sensitive. But the comparison you have set up doesn't actually check any results. This:

!empty($count)

is just checking if $count has any value. Which it does. Whether that value is a positive number or not, your code doesn't care. But it probably should...

David
  • 208,112
  • 36
  • 198
  • 279
  • Thank You David, I missed that and that seems to have done the trick, talk about not seeing the trees for the forest. – Guy McLaren Jun 24 '16 at 13:05