Jump to content

listing table name - is it possible?


wgoldschagg

Recommended Posts

Hi again guys,

 

I am busy trying to write my own blog script, and in case youre wondering i have a pretty good idea of what i want to do, but i dont want to copy code from other scripts.  Is it possible to list the names of tables in a database as variables with some mysql query?

Link to comment
https://forums.phpfreaks.com/topic/158801-listing-table-name-is-it-possible/
Share on other sites

In the PHP/MYSQL documentation, try this

 

<?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);
?>

 

or...

 

<?php
mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("mydb");
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
    echo "Table: ", mysql_tablename($result, $i), "\n";
}

mysql_free_result($result);
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.