1

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?

4

2 回答 2

1

您的 SQL 语法错误。检查变量 $table 的值。你应该有类似的东西

SHOW TABLES LIKE "%"
于 2013-09-03T20:19:47.517 回答
0

我省略了引号。

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

或者

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

或者

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

或者

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

谢谢你的帮助。

于 2013-09-05T12:47:48.950 回答