Query for Tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Query for Tables

Post by AliasBDI »

I am wanting to get a dynamic list of TABLES in a Database. Is this possible? How would it look if it is possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SHOW TABLES
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Fair enough. I'm trying to get the list of the tables into a multiple select form field:

Code: Select all

<select name="tablesї]" size="6" multiple="multiple" class="forms" id="tablesї]">
                  <option value="con_news">var_month</option>
                  <?php
do {  
?>
                  <option value="<?php echo $row_tableLISTї'id']?>"><?php echo $row_tableLISTї'id']?></option>
                  <?php
} while ($row_tableLIST = mysql_fetch_assoc($tableLIST));
  $rows = mysql_num_rows($tableLIST);
  if($rows > 0) {
      mysql_data_seek($tableLIST, 0);
	  $row_tableLIST = mysql_fetch_assoc($tableLIST);
  }
?>
Here is my query and connection code:

Code: Select all

<?php mysql_select_db($*****, $*******);
$query_tableLIST = "SHOW TABLES";
$tableLIST = mysql_query($query_tableLIST, $*****) or die(mysql_error());
$row_tableLIST = mysql_fetch_assoc($tableLIST);
$totalRows_tableLIST = mysql_num_rows($tableLIST); 

?>
I put **** on db name, disregard them.

I guess my question is, how would I echo the results?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's like any standard query for multiple records; run it in a fetching loop ;)
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I got it. Thanks for the help. I was confused because I thought that there should have been an echo field name. But that cleared it up. Thanks for the help again.

Here is the code for you readers:
QUERY CODE:

Code: Select all

<?php
mysql_select_db($databasename, $connect);
$query_queryLIST = "SHOW TABLES";
$queryLIST = mysql_query($query_queryLIST, $connect) or die(mysql_error());
$row_queryLIST = mysql_fetch_row($queryLIST);
$totalRows_queryLIST = mysql_num_rows($queryLIST);

mysql_free_result($queryLIST);
?>
ECHO RESULTS CODE:

Code: Select all

<?php
<?php
do {  
?>
                  <option value="<?php echo $row_queryLIST[0]; ?>"><?php echo $row_queryLIST[0]; ?></option>
                  <?php
} while ($row_queryLIST = mysql_fetch_row($queryLIST));
  $rows = mysql_num_rows($queryLIST);
  if($rows > 0) {
      mysql_data_seek($queryLIST, 0);
	  $row_queryLIST = mysql_fetch_row($queryLIST);
  }
?>
Post Reply