PHP MySQL Functions to list table names in a databases

We can list tables present in a database by using this query
SHOW TABLES
Using this we can develop a PHP script to display all tables.
<?Php
require "config.php"; // Database Connection

$result = $dbo->query("SHOW TABLES");

while ($row = $result->fetch(PDO::FETCH_NUM)) {
echo $row[0]."<br>";
}
?>

Show tables of a database

We can display tables of a database by changing the above query.
SHOW TABLES in PDO
Above query will return all the tables of the database pdo
Similarly if we want to list all the tables starting with char t then we can use LIKE command. Here is the query.
SHOW TABLES from pdo LIKE 't%'
Above query can be placed inside the PHP code (shown above ) to display matching tables. For easy understanding here again the php code.
<?Php
require "config.php"; // Database Connection

$result = $dbo->query("SHOW TABLES from pdo LIKE 't%'");

while ($row = $result->fetch(PDO::FETCH_NUM)) {
echo $row[0]."<br>";
}
?>
Using MySQLI
if($stmt = $connection->query("SHOW TABLES")){
  echo "No of records : ".$stmt->num_rows."<br>";
  while ($row = $stmt->fetch_array()) {
echo $row[0]."<br>";
  }
}else{
echo $connection->error;
}

mysql_list_tables()

We can display a list of tables present in a database by using mysql_list_tables() function. Connection has to be established before call of this function. The result pointer returned by this function is used by mysql_tablename() function to display the name of the tables.
<?Php
$list = mysql_list_tables ("sql_tutorial");
$i = 0;
while ($i < mysql_num_rows ($list)) {
$tb_names[$i] = mysql_tablename ($list, $i);
echo $tb_names[$i] . "<BR>";
$i++;
}
?>

PHP MySQL functions SHOW Databases

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com
    Vicente Carlos

    20-05-2012

    I1m loving your site! Very usefull. Thanks
    ram

    02-11-2012

    this website is very useful. thank you
    Deepak

    22-04-2015

    Helpful website. But Please also include a demo of code if possible. Thank you for helping coders.

    Post your comments , suggestion , error , requirements etc here





    SQL Video Tutorials










    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer