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?

Julius Rickert
  • 410
  • 3
  • 13

2 Answers2

1

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

SHOW TABLES LIKE "%"
Flavio
  • 846
  • 1
  • 9
  • 21
  • I'm not the downvoter but % is not a regular expression and this code will return all the tables not just the one that the user is look for. – Orangepill Sep 03 '13 at 20:30
  • Well sure, I could have used "." or "*" for what matters... It's just a wildcard I've used instead of $table... If $table=% he will get the list of all tables, if $table=whatever he will get just that table. Ok nvm... – Flavio Sep 03 '13 at 20:35
0

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.

Julius Rickert
  • 410
  • 3
  • 13