In this tutorial you can learn how to check with PHP if one, or multiple tables exist into a MySQL database.
The SQL command that returns a list with all the tables in database is this:
SHOW TABLES
OR:
SHOW TABLES IN `database_name`
- This query returns the name of the tables in a database, the result is stored into an alias named Tables_in_databasename ; databasename is the name of the database.

For example, if we have a MySQL database named "tests", with these tables: users, temp, and online; we can use the following code in PHP to get a list with the table's name:
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if (mysqli_connect_errno()) {
 exit('Connect failed: '. mysqli_connect_error());
}

// SQL query
$sql = "SHOW TABLES IN `tests`";

// perform the query and store the result
$result = $conn->query($sql);

// if the $result not False, and contains at least one row
if($result !== false) {
 // if at least one table in result
 if($result->num_rows > 0) {
 // traverse the $result and output the name of the table(s)
 while($row = $result->fetch_assoc()) {
 echo '<br />'. $row['Tables_in_tests'];
 }
 }
 else echo 'There is no table in "tests"';
}
else echo 'Unable to check the "tests", error - '. $conn->error;

$conn->close();
?>
This code will return the following result:
users
temp
online

If you want to check if a certain table exists into a MySQL database, you can use this SQL query:
SHOW TABLES IN `databasename` WHERE `Tables_in_databasename` = 'table_name'

- For example, to check if the "users" table exists in the "tests" database, we can use this code in PHP:
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if (mysqli_connect_errno()) {
 exit('Connect failed: '. mysqli_connect_error());
}

// SQL query
$sql = "SHOW TABLES IN `tests` WHERE `Tables_in_tests` = 'users'"; 

// perform the query and store the result
$result = $conn->query($sql);

// if the $result not False, and contains at least one row
if($result !== false) {
 // if the $result contains at least one row, the table exists, otherwise, not exist
 if ($result->num_rows > 0) echo 'The table "users" exists';
 else echo 'The table "users" Not exists';
}
else echo 'Unable to check the "tests", error - '. $conn->error;

$conn->close();
?>

Function to check if one ore multiple tables exist in a database

The following function can be used in PHP to see if one, or multiple tables exists in a MySQL database.
// check if tables passed in $tables exists in $db
function tableExist($conn, $db, $tables) {
 $eror = false;

 // if $tables is a string, convert it into an Array
 if(is_string($tables)) $tables = array($tables);

 $result=$conn->query("SHOW TABLES IN `$db`");
 if($result !== false) {
 // if at least one table in result
 if($result->num_rows > 0) {
 // traverse the $result and store all tables into an array
 while($row = $result->fetch_assoc()) {
 $tables_db[] = $row['Tables_in_'.$db];
 }

 // check if each table from $tables is in $tables_db, if not, sets error
 for($i=0; $i<count($tables); $i++) {
 if(!in_array($tables[$i], $tables_db)) $eror[] = 'Table '. $tables[$i]. ' Not exist in "'. $db. '"';
 }
 }
 else $eror[] = 'There is no table in "'. $db. '"';
 }
 else $eror[] = 'Unable to check the "'. $db. '"';

 // if $eror not False, output errors and returns false, otherwise, returns true
 if($eror !== false) {
 echo implode('<br/>', $eror);
 return false;
 }
 else return true;
}
The tableExist() function receives three arguments:
- $conn - is the object that contains the connection to MySQL.
- $db - represents the name of the database.
- $tables - can be a string with the name of a table, or an Array with multiple table names.

• Example, check if the tables: "comments", "temp", and "users" exist in database "tests".
<?php
// Free WebMaster courses - https://coursesweb.net

// check if tables passed in $tables exists in $db
function tableExist($conn, $db, $tables) {
 $eror = false;

 // if $tables is a string, convert it into an Array
 if(is_string($tables)) $tables = array($tables);

 $result=$conn->query("SHOW TABLES IN `$db`");
 if($result !== false) {
 // if at least one table in result
 if($result->num_rows > 0) {
 // traverse the $result and store all tables into an array
 while($row = $result->fetch_assoc()) {
 $tables_db[] = $row['Tables_in_'.$db];
 }

 // check if each table from $tables is in $tables_db, if not, sets error
 for($i=0; $i<count($tables); $i++) {
 if(!in_array($tables[$i], $tables_db)) $eror[] = 'Table '. $tables[$i]. ' Not exist in "'. $db. '"';
 }
 }
 else $eror[] = 'There is no table in "'. $db. '"';
 }
 else $eror[] = 'Unable to check the "'. $db. '"';

 // if $eror not False, output errors and returns false, otherwise, returns true
 if($eror !== false) {
 echo implode('<br/>', $eror);
 return false;
 }
 else return true;
}

$db = 'tests';
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', $db);

// check connection
if (mysqli_connect_errno()) {
 exit('Connect failed: '. mysqli_connect_error());
}

// array with the tables to test
$tables = array('comments', 'temp', 'users');

// calls the tableExist() function
if(tableExist($conn, $db, $tables)) echo 'All the tables: <i>'. implode(', ', $tables). '</i> exists';
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Check if table exists in database

Last accessed pages

  1. Get the value of the selected /checked checkboxes in a form (41519)
  2. jQuery Drag and Drop Rows between two similar Tables (8474)
  3. JQZoom Image Magnifier (10575)
  4. Contact page - CoursesWeb (22089)
  5. Tabs effect with CSS (22791)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (1761)
  2. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1643)
  3. Contact page - CoursesWeb (1589)
  4. Tabs effect with CSS (1586)
  5. Insert, Select and Update NULL value in MySQL (1049)