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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.