Jump to content

Archived

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

nadeemshafi9

foreach ( table in db)

Recommended Posts

hi

is there a way of performing a foreach(table as i){} on tables in a mysql db using php if so i cant locate the code plz hlp.

thanks alot

nadeem

Share this post


Link to post
Share on other sites
i can sort of see how that could work but would it perform a fetch array on each table automaticaly?

what im trying to do is read the info in each table and create html interfaces using fopen(file, W), a auto page generate

Share this post


Link to post
Share on other sites
i dont fully get what your wanting to acomplish it all depends on your query, not the method of it being returned

Share this post


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

Share this post


Link to post
Share on other sites
this is what i got so far i dosent work

foreach(mysql_list_tables("wellmeadows") as $tableName) {
print $tableName;
}

Share this post


Link to post
Share on other sites
hay man i got it thanx alot thogh

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

while ($row = mysql_fetch_row($result)) {
  echo "Table: {$row[0]}\n";
}

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
yes thanx alot i dint know about not embeding them, hay how cums people use echo instead of print?

thanx for ur help

v greatfull

Share this post


Link to post
Share on other sites
differences between echo and print
http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

99% of the time you can use them interchangeably, as most code does not require the use of the finer aspects of the 2.

Share this post


Link to post
Share on other sites

×

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.