简体   繁体   中英

How to retrieve all table names from database?

I'm trying to retrieve all table names from a MySQL database and link them to a dropdown list. Then I want to store those table names in a table and preview them.

here's my code...

<?php 
$sql = "SHOW TABLES FROM $ip";
$result = mysql_query($sql);
?>
<form id="form1" name="form1" method="post" action="newloay.php">
<select name="select" id="select" required>
    <?php
     while($row =  mysql_fetch_array($result)){
    ?>
        <option> <?php print $row[0] ?> </option>
    <?php 
     }  ?>

    }
</select>

MySQL :

SELECT 
  table_name
FROM 
  my_schema.tables

Where my_schema is your schema name , $ip , I guess.

Follow the following code to get the Name of the Tables in your Database :

<?php
$dbname = 'mysql_dbname';

if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    echo "Table: {$row[0]}\n";
}

mysql_free_result($result);
?>

Hope this helps, for more details on this, please check out THIS PAGE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM