php mysql table exists function

this checks if a mysql table already exists in the selected database and returns true or false

Demo

<?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
Page Views

This page has been viewed 47 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.

❤👩‍💻🕹