10

Got a bit of PHP code I'm struggling with - had a search around Google etc. and tried everything mentioned, but for some reason I'm having trouble solving it.

The problem is:

I have some code that is querying a database for the presence of a particular user.

The code (it's a method inside a class)

<?php
global $mysqli;
// Query specified database for value
$q = 'SELECT id FROM ' . $database . ' WHERE username = \'' . $username . '\'';
$r = $mysqli->query($q);
var_dump($r);
if ($r->num_rows) {
    // If row found username exists so return false
    return false;
}
...
?>

I've var dumped the result of the query ($r) and got this:

object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

This is correct, there should only be 1 row as above.

I do get this error linking to the line saying if ($r->num_rows) {

Notice: Trying to get property of non-object in FILE on line LINE

but I don't know why since the object is valid (as above) and it should be working fine. From what I can tell it seems to be going through alright, I'm just wondering why there's an error. I'm sure it's something simple but I'd appreciate any help.

Rick Hanshaw
  • 141
  • 1
  • 1
  • 4
  • 1
    That's indeed very strange, try to `var_dump($r->num_rows)`. BTW: If you are using mysqli, why aren't you using prepared statements or at least escape the inserted values? – feeela Sep 07 '11 at 11:13
  • did u try $r["num_rows"] – Roshan Wijesena Sep 07 '11 at 11:14
  • This is just in early development at the moment so I haven't added any escaping etc., but as soon as it's released it will all be added accordingly. Any ideas on my actual problem? – Rick Hanshaw Sep 07 '11 at 11:16
  • You are using global, make me guess you are passing the $r to another file for processing – ajreal Sep 07 '11 at 11:17
  • @Haim yes I used $mysqli = new mysqli('localhost', 'user', '', 'db'); – Rick Hanshaw Sep 07 '11 at 11:17
  • @Roshan [according to the manual](http://www.php.net/manual/en/mysqli-result.num-rows.php), the notation from the question is correct – feeela Sep 07 '11 at 11:17
  • I also get `int(1)` when I add `var_dump($r->num_rows)` So it seems the int(1) is coming through, so I have no idea why a message is appearing?! – Rick Hanshaw Sep 07 '11 at 11:19
  • It seems to be working when I change return false in the If statement to anything else (i.e. return true etc.). Anyone know anything behind why? – Rick Hanshaw Sep 07 '11 at 11:25
  • use isset function for check in if statement : if (isset($r->num_rows)) – mamal Mar 02 '20 at 12:16

5 Answers5

32
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

if (!$result) {
    trigger_error('Invalid query: ' . $conn->error);
}

check the error with mysqli_error() function

probably your query has some faults.

smassey
  • 5,875
  • 24
  • 37
kemalatila
  • 419
  • 5
  • 13
2

The cause of your problem is simple. So many people will run into the same problem, Because I did too and it took me hour to figure out. Just in case, someone else stumbles, The problem is in your query, your select statement is calling $dbname instead of table name. So its not found whereby returning false which is boolean. Good luck.

abpatil
  • 916
  • 16
  • 20
-1

I have been working on to write a custom module in Drupal 7 and got the same error:

Notice: Trying to get property of non-object

My code is something like this:

function example_node_access($node, $op, $account) {
  if ($node->type == 'page' && $op == 'update') {
    drupal_set_message('This poll has been published, you may not make changes to it.','error');
    return NODE_ACCESS_DENY;    
  }
}

Solution: I just added a condition if (is_object($sqlResult)), and everything went fine.

Here is my final code:

function mediaten_node_access($node, $op, $account) {

    if (is_object($node)){

     if ($node->type == 'page' && $op == 'update') {
       drupal_set_message('This poll has been published, you may not make changes.','error');
       return NODE_ACCESS_DENY;    
      }

    }

}
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Sashi Kiran
  • 1
  • 1
  • 1
-1

I think thats not the reason everybody told above. There is something wrong in your code, maybe miss spelling or mismatching with the database column names. If mysqli query gets no result then it will return false, so that it is not a object - is a wrong idea. Everything works fine. it returns 1 or 0 if query have result or not.

So, my suggestion is check your variable names and table column names or any other misspelling.

Xuwel Khan
  • 294
  • 4
  • 10
-2

Just thought I would expand on this a bit.

If you perform a MYSQLI SELECT query that returns 0 results, it returns FALSE.

However, if you get this error and you have written your own MYSQLI Query function, then you can also get this error if the query you are running is not a select but an update. An update query will return either TRUE or FALSE. So if you just assume that any non false result will have records returned, then you will trip up when you run an update or anything other than select.

The easiest solution, once you have checked that its not false, is to first check that the result of the query is an object.

    $sqlResult = $connection->query($sql);
    if (!$sqlResult)
    {
     ... 
    }
    else if (is_object($sqlResult))
    {
        $sqlRowCount = $sqlResult->num_rows;
    }
    else
    {
        $sqlRowCount = 0;
    }
Relaxing In Cyprus
  • 1,976
  • 19
  • 25
  • The first line of this answer is untrue. It only returns `FALSE` when there's an error. A query with 0 matches is not an error. – Barmar Jul 14 '18 at 04:46
  • You missed the bit where I said you were running an UPDATE, not a SELECT. – Relaxing In Cyprus Jul 14 '18 at 10:08
  • Sorry, I didn't look closely at the question. I thought you were talking about `mysqli::query()`, not a user-written method with its own criteria for returning `FALSE`. – Barmar Jul 14 '18 at 13:49