wgoldschagg Posted May 19, 2009 Share Posted May 19, 2009 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 More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 SQL - SHOW TABLES; Link to comment https://forums.phpfreaks.com/topic/158801-listing-table-name-is-it-possible/#findComment-837559 Share on other sites More sharing options...
wgoldschagg Posted May 19, 2009 Author Share Posted May 19, 2009 thanks. Link to comment https://forums.phpfreaks.com/topic/158801-listing-table-name-is-it-possible/#findComment-837574 Share on other sites More sharing options...
radi8 Posted May 19, 2009 Share Posted May 19, 2009 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); ?> Link to comment https://forums.phpfreaks.com/topic/158801-listing-table-name-is-it-possible/#findComment-837581 Share on other sites More sharing options...
wgoldschagg Posted May 19, 2009 Author Share Posted May 19, 2009 Thank you very much for your help. Link to comment https://forums.phpfreaks.com/topic/158801-listing-table-name-is-it-possible/#findComment-837596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.