Forum Moderators: coopster

Message Too Old, No Replies

Display tables in the current database

         

shams

2:00 am on May 16, 2007 (gmt 0)

10+ Year Member



hi,
i just write this script to display the tables of current database but this is not working:
<?php
include 'library/config.php';
include 'library/opendb.php';
$query="show tables as 'tables'";
$res = mysql_query($query);

while($row=mysql_fetch_array($res)) {
echo $row['tables']
};
include 'library/closedb.php';
?>

eelixduppy

2:16 am on May 16, 2007 (gmt 0)



Try something like this:

$query = 'show tables';
$result = mysql_query($query);
#
$tables = array();
while($row = mysql_fetch_array($result)) {
$tables[] = $row[0];
}
echo '<pre>';
print_r($tables);
echo '</pre>';

shams

2:26 am on May 16, 2007 (gmt 0)

10+ Year Member



hi eelixduppy,
thanks for reply this is the code:
<?php
include 'library/config.php';
include 'library/opendb.php';

$query = 'show tables';
$result = mysql_query($query);
#
$tables = array();
while($row = mysql_fetch_array($result)) {
$tables[] = $row[0];
}
echo '<pre>';
print_r($tables);
echo '</pre>';

include 'library/closedb.php';
?>
and this is the output:
Array
(
)

eelixduppy

2:32 am on May 16, 2007 (gmt 0)



Works for me. Must be something on your end. Are you properly connecting to the mysql server AND selecting the database? Are there actually tables in the database? ;) Check these first.

shams

4:03 pm on May 16, 2007 (gmt 0)

10+ Year Member



thanks so much it works like:
Array
(
[0] => register
[1] => treatment
)

can we change the output to:
[0] => register
[1] => treatment

eelixduppy

1:22 am on May 17, 2007 (gmt 0)



$tables
is an array containing the names of the tables in that database. The code I have shown you just echos the contents of the array. If you want to do something else with them, you can loop [php.net] through the array and echo accordingly.