php mysql table exists function

this checks if a mysql table already exists in the selected database and returns true or false
<?php
// setup database connection
$db = new mysqli($db_host$db_user$db_pass$db_name);

// function
function table_exists($table) {
  global 
$db;
  
$table $db->mysql_real_escape_string($table);
  
$sql "SHOW TABLES LIKE '$table' ";
  
$result $db->query($sql);
  
$num 0;
  
$num mysqli_num_rows($result);
  if(
$num 0) {
      return 
true;
  }
  return 
false;
}

// usage
$my_table_name "my_table_name";
if(
table_exists($table $my_table_name)) {
  echo 
"$my_table_name exists :)";
} else {
  echo 
"$my_table_name does not exist :(";
}
?>

Other Items in php
Related Search Terms
Page Views

This page has been viewed 564 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

âĪðŸ‘Đ‍ðŸ’ŧðŸ•đ

Random Quote
therock Somewhere along this crazy road I learned (often times the hard way) the most important things I can do is be authentic, trust my gut, be the hardest worker in the room, celebrate the mistakes, be a grateful man and always remember that it's nice to be important, but it's more important to be nice.
The Rock