Julius Rickert
Julius Rickert

Reputation: 430

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?

Upvotes: 1

Views: 2367

Answers (2)

Julius Rickert
Julius Rickert

Reputation: 430

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.

Upvotes: 0

Flavio
Flavio

Reputation: 866

Your SQL syntax is wrong. Check the value of your variable $table. You should have something like

SHOW TABLES LIKE "%"

Upvotes: 1

Related Questions