Jump to content


Photo

foreach ( table in db)


  • Please log in to reply
13 replies to this topic

#1 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 01:55 PM

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
DONT LAY AN EGG OVER IT

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 27 August 2006 - 02:07 PM

why not use

while ($row=mysql_fetch_array($query))  { } ???



Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 03:02 PM

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
DONT LAY AN EGG OVER IT

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 27 August 2006 - 03:36 PM

i dont fully get what your wanting to acomplish it all depends on your query, not the method of it being returned
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:07 PM

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
DONT LAY AN EGG OVER IT

#6 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:11 PM

this is what i got so far i dosent work

foreach(mysql_list_tables("wellmeadows") as $tableName) {
print $tableName;
}
DONT LAY AN EGG OVER IT

#7 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:17 PM

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";
}
DONT LAY AN EGG OVER IT

#8 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:18 PM

mysql_list_tables() is depreciated its out of date SQL has it built in
DONT LAY AN EGG OVER IT

#9 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 August 2006 - 05:24 PM

this is what i would do:

<?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
}
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:39 PM

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
DONT LAY AN EGG OVER IT

#11 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 August 2006 - 05:49 PM

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

expanding on the code i provided:
<?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>";
   }
}
?>


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#12 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 05:53 PM

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
DONT LAY AN EGG OVER IT

#13 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 August 2006 - 06:06 PM

differences between echo and print
http://www.faqts.com...ml/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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#14 nadeemshafi9

nadeemshafi9
  • Members
  • PipPipPip
  • Advanced Member
  • 1,245 posts
  • LocationUK

Posted 27 August 2006 - 07:04 PM

thanx m8 il post back if i hav any further probs thanx alot
DONT LAY AN EGG OVER IT




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users