Can anyone tell me how can I list the table names from a database ??
I tried doing it but failed. By the way I want the table name to appear in a drop down list.
Thanks in advance
-Omair
| SitePoint Sponsor |




Can anyone tell me how can I list the table names from a database ??
I tried doing it but failed. By the way I want the table name to appear in a drop down list.
Thanks in advance
-Omair
36Host.com - $36/year web hosting [affiliates earn 30%]
* Affordable Small Business Web Hosting since 2003! *
"500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
mysql, pre-installed php scripts, 24/7 support & more...."




Also, I tried using this code but all it does is displays the same table twice.
BTW, there is only one table in the database.
Anyways here is the code:
PHP Code:<?php
$host="localhost";
$username="";
$password="";
$database="jokeave";
$list = mysql_list_tables($database);
$result = mysql_fetch_array($list)
?>
<select name="select">
<?php
do {
$val=current($result);
echo "<option>$val</option>";
} while (next($result));
?>
</select>
36Host.com - $36/year web hosting [affiliates earn 30%]
* Affordable Small Business Web Hosting since 2003! *
"500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
mysql, pre-installed php scripts, 24/7 support & more...."


PHP Code:<select name="select">
<?php
$query = mysql_list_tables("databasename");
for ($i=0; $i < mysql_num_rows($query);) {
$result = mysql_tablename($query, $i++);
echo "<option>$result\n";
}
?>
</select>





Try this,
PHP Code:$list=mysql_list_tables($database);
while ( list($name) = mysql_fetch_array($list)) {
echo "$name<br>";
}



i had to write a script recently which loops through a database, gets the tablenames and lists below it the fields in the table
Hope this helps,PHP Code:<?php
mysql_connect("localhost","xxxxxx","xxxxxxx");
$DB = "hanswer_forums";
mysql_select_db($DB);
$tables=mysql_list_tables($DB);
while (list($bla)=mysql_fetch_array($tables))
{
echo "<br><font face=verdana,arial size=2><b>Table: $bla</b>";
echo "<br>";
$fields = mysql_list_fields("$DB", "$bla");
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
echo mysql_field_name($fields, $i) . "<br>";;
}
}
?>
Alex
Bookmarks