ShaolinF Posted June 17, 2008 Share Posted June 17, 2008 Hi Guys How can I find all the tables in my database? Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/ Share on other sites More sharing options...
MasterACE14 Posted June 17, 2008 Share Posted June 17, 2008 mysql_list_tables — List tables in a MySQL database resource mysql_list_tables ( string $database [, resource $link_identifier ] ) Retrieves a list of table names from a MySQL database. http://au2.php.net/mysql Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-567699 Share on other sites More sharing options...
ShaolinF Posted June 18, 2008 Author Share Posted June 18, 2008 Thanks Everytime I try it I get a mydb could not be converted to string. $sql = "SHOW TABLES FROM $mydb" or die (mysql_error()); $result = mysql_query($sql); while ($row = mysql_fetch_row($result)) { echo "Table: {$row[1]}\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-567703 Share on other sites More sharing options...
Pikachu2000 Posted June 18, 2008 Share Posted June 18, 2008 Thanks Everytime I try it I get a mydb could not be converted to string. $sql = "SHOW TABLES FROM $mydb" or die (mysql_error()); $result = mysql_query($sql); while ($row = mysql_fetch_row($result)) { echo "Table: {$row[1]}\n"; } Assuming you have the $mydb variable's value set elsewhere, this should work for you. $query = "SHOW TABLES FROM " . $mydb; $result = mysql_query( $query ); while( $array = mysql_fetch_array( $result, MYSQL_ASSOC) ) { print_r( $array ); } Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-567831 Share on other sites More sharing options...
ShaolinF Posted June 19, 2008 Author Share Posted June 19, 2008 thanks. That gives me the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-568686 Share on other sites More sharing options...
hitman6003 Posted June 19, 2008 Share Posted June 19, 2008 What is $mydb? Is it a string, e.g. the name of the database? Or is it the result of a mysql_connect operation (which would make it a resource object)? Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-568687 Share on other sites More sharing options...
ShaolinF Posted June 19, 2008 Author Share Posted June 19, 2008 It is a mysql connect object. Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-568711 Share on other sites More sharing options...
Pikachu2000 Posted June 19, 2008 Share Posted June 19, 2008 It is a mysql connect object. If it's just the database name, it should work in the query, if it is a $mydb = mysql_connect( $db_host, $db_user, $db_pass ); statement, it will not. That means you will have to use a variable containing only the database name. Try echoing the $sql variable to see what you are sending as a db query. Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-568783 Share on other sites More sharing options...
ShaolinF Posted June 19, 2008 Author Share Posted June 19, 2008 It gives me an error. Basically the $mydb is an object oriented instance. When I want to call a table from the DB I use $mydb->tableName - What Im trying to do is to get all the db tables to display but since $mydb is an object the I don't know how to grab all the table names and output them.. Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-569324 Share on other sites More sharing options...
ShaolinF Posted June 20, 2008 Author Share Posted June 20, 2008 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-569729 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2008 Share Posted June 20, 2008 Anyone? Replace the $mydb variable in the query by explicitly stating the database name and see if it gives you anything. What is the error you are getting now? Quote Link to comment https://forums.phpfreaks.com/topic/110660-finding-tables-in-db/#findComment-569739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.