Search code examples
phpmysqlimysql-num-rows

PHP: Trying to get property of non-object


I am working on a function, what returns whether a table exists or not.

But it always notices:

Notice: Trying to get property of non-object [...] on line 10

in

1  function table_exists($table) {
2      
3      // get the database
4      global $mysqli;
5      
6      // look for tables named $table
7      $result = $mysqli->query("SHOW TABLES LIKE $table");
8      
9      // if the result has more than 0 rows
10     if($result->num_rows > 0) {
11         return true;
12     } else {
13         return false;
14     }
15 }

the $mysqli var is set like this:

$mysqli = new mysqli(mysqli_host, mysqli_user, mysqli_password, mysqli_database);

How to solve that?


Solution

  • I left out the quotes.

    $result = $mysqli->query("SHOW TABLES LIKE \"$table\"");
    

    or

    $result = $mysqli->query("SHOW TABLES LIKE '$table'");
    

    or

    $result = $mysqli->query("SHOW TABLES LIKE \"" . $table . "\"");
    

    or

    $result = $mysqli->query("SHOW TABLES LIKE '" . $table . "'");
    

    thanks for your help.