Scanning a database for tables<

liunx

Guest
I am trying to do something fairly simple. I want to do a simple "SHOW TABLES" on a database to get a list of tables in that database. The problem is I don't know which function to use to view the query results:

PHP:
$conn = mysql_pconnect("localhost","username","pass");
if(!$conn)  {
   print "Could not connect to the database<br>\n";
   exit;
}

mysql_select_db("racing",$conn);
//set up a query to get all data from the table

$sql = "SHOW TABLES";
$result = mysql_query($sql,$conn);
$tables=mysql_fetch_array($result);
$effected=mysql_num_rows($result);
print "0: ".$tables[0]."<br>";
print "1: ".$tables[1]."<br>";
print "2: ".$tables[2]."<br>";
print "3: ".$tables[3]."<br>";
print "4: ".$tables[4]."<br>";
</PHP]

When I print out $effected I get 5, which is the correct number of tables, but when I try to print out the tables I only get the first one.  I'm obviously not using the correct fetch routine.Not sure but this might work:

$conn = mysql_pconnect("localhost","username","pass");
if(!$conn) {
print "Could not connect to the database<br>\n";
exit;
}

mysql_select_db("racing",$conn);
//set up a query to get all data from the table

$sql = "SHOW TABLES";
$result = mysql_query($sql,$conn);
$effected=mysql_num_rows($result);

$i = 0;
while( $tables=mysql_fetch_row($result) )
{
print $i . ": ".$tables[0]."<br>";
$i++;
}Just to add to that code of yours IKLOP, use mysql_close() when using a persistant connection.


$conn = mysql_pconnect("localhost","username","pass"); 
if(!$conn) { 
print "Could not connect to the database<br>\n"; 
exit; 
} 

mysql_select_db("racing",$conn); 
//set up a query to get all data from the table 

$sql = "SHOW TABLES"; 
$result = mysql_query($sql,$conn); 
$effected=mysql_num_rows($result); 

$i = 0; 
while( $tables=mysql_fetch_row($result) ) 
{ 
print $i . ": ".$tables[0]."<br>"; 
$i++; 
}

// close the persistant connection
mysql_close($conn);There's no need to use mysql_close()I didn't think to get multiple rows.  For some reason I thought there was a query routine which returned a string that was equivalent to what the command line editor would return.Originally posted by Josh 
There's no need to use mysql_close()  

True...

Note: mysql_close() will not close persistent links created by mysql_pconnect().Speaking of a persistent connection.  Can someone give me an example of where you'd need a persistent connection?  I don't know if I should use it or not.to quote php.net

 mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()). 

<!-- m --><a class="postlink" href="http://us3.php.net/manual/en/function.mysql-pconnect.phpOriginally">http://us3.php.net/manual/en/function.m ... Originally</a><!-- m --> posted by IKLOP 
Not sure but this might work:

$conn = mysql_pconnect("localhost","username","pass");
if(!$conn) {
print "Could not connect to the database<br>\n";
exit;
}

mysql_select_db("racing",$conn);
//set up a query to get all data from the table

$sql = "SHOW TABLES";
$result = mysql_query($sql,$conn);
$effected=mysql_num_rows($result);

$i = 0;
while( $tables=mysql_fetch_row($result) )
{
print $i . ": ".$tables[0]."<br>";
$i++;
}
  
yup, that should work just fine.to quote php.net 

Yeah I read the description but I'm trying to figure out what application pconnect is meant for.  Would it be smarter to use it when you have lots of people trying to connect at the same time?  If only one user is accessing the database is there even a point to using pconnect?
 
Back
Top