Forum Moderators: coopster

Message Too Old, No Replies

MySQL Tables Array?

How to get all tables into an Array...

         

ahmedtheking

12:18 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to run such a query:

SHOW TABLES

In php, via mysql_query and get the tables as an array?

The purpose of this is so that I can run the same query on seperate tables without having to rewrite the query for each table!

anshul

12:49 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



SHOW TABLES is perhaps an exception to mysql_query()
SHOW DATABASES, but works.
U may c PHPMyAdmin, if u've ( code ); how its lists tables!

ahmedtheking

1:03 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But is there a way to stick the table names into an array?

coopster

1:58 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can get any data back from a mysql_query you want. The easiest way to find out what is coming back and how it comes back is to dump the $row returned array.

<pre> 
<?php
$rows = mysql_query("SHOW TABLES FROM test");
while ($row = mysql_fetch_assoc($rows)) {
print_r($row);
}
?>
</pre>

So, knowing what is coming back, you design your query ...

<pre> 
<?php
$tables = array();
$rows = mysql_query("SHOW TABLES FROM test");
while ($row = mysql_fetch_array($rows)) {
$tables[] = $row[0];
}
print_r($tables);
?>
</pre>

anshul

6:36 am on Apr 30, 2005 (gmt 0)

10+ Year Member



> You can get any data back from a mysql_query you want.

It is sometimes useful to get all information on a Web page, as given at [U]command prompt[/U] by MySQL daemon.
Like '5 records updated', '1 row inserted', 'not found', 'query took 0.018 sec' etc. Can? someone tell to get this all infomation in PHP.

ahmedtheking

10:47 am on Apr 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah nice one, I'll give that a try!

Stormfx

5:11 am on May 1, 2005 (gmt 0)

10+ Year Member



anshul

Try:

[php.net...]

&

[php.net...]