Jump to content

foreach ( table in db)


nadeemshafi9

Recommended Posts

what im trying to do is reguardless of the DB, turn by turn get the table names and all the names of the feilds and then write a php file using fopen() that will allow the user to veiw and alter the tables, A kind of CMS.

well iv come accross mysql_list_tables()

pretend $i = mysql_list_tables($db)

can u tell me how i can get the names of the tables out of $i whith print, i know its easy but i cant rember.

thanx for any help
Link to comment
https://forums.phpfreaks.com/topic/18801-foreach-table-in-db/#findComment-81161
Share on other sites

this is what i would do:

[code]
<?php
//connect to db

$sql = "SHOW TABLES FROM dbname";
$result = mysql_query($sql);

while ($list = mysql_fetch_row($result)) {
  $tablelist[] = $list[0];
}

foreach ($tablelist as $tablename) {
  //do your table query using $tablename as the table name
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/18801-foreach-table-in-db/#findComment-81171
Share on other sites

Ok im on the second prob know im trying to get the feild names of the table dynamicaly

igot this so far

$sql = "SELECT * FROM ".$tablename;

while($fn = mysql_field_name(mysql_query($sql),0))
{
print $fn;
}

the error says that my result resourse is not valid i wonder why itryed executing it first in to a var then putting iit in to mysql_field_name($result, 0)

thanks again
Link to comment
https://forums.phpfreaks.com/topic/18801-foreach-table-in-db/#findComment-81179
Share on other sites

okay first off, you shouldn't imbed sql functions inside each other like that. 

expanding on the code i provided:
[code]
<?php
//connect to db

//get list of tables in database
$sql = "SHOW TABLES FROM dbname";
$result = mysql_query($sql);

//put the list in an array
while ($list = mysql_fetch_row($result)) {
  $tablelist[] = $list[0];
}

//for each table...
foreach ($tablelist as $tablename) {
  //get list of columns and echo them
  $sql = "SHOW COLUMNS FROM $tablename";
  $result = mysql_query($sql);
  echo "<b>$tablename:</b><br>";
  while ($list = mysql_fetch_row($result)) {
      echo $list[0] . "<br>";
  }
}
?>
[/code]

Link to comment
https://forums.phpfreaks.com/topic/18801-foreach-table-in-db/#findComment-81183
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.