nadeemshafi9 Posted August 27, 2006 Share Posted August 27, 2006 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 alotnadeem Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 27, 2006 Share Posted August 27, 2006 why not usewhile ($row=mysql_fetch_array($query)) { } ???Liam Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 27, 2006 Share Posted August 27, 2006 i dont fully get what your wanting to acomplish it all depends on your query, not the method of it being returned Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 this is what i got so far i dosent work foreach(mysql_list_tables("wellmeadows") as $tableName) { print $tableName;} Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 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"; } Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 mysql_list_tables() is depreciated its out of date SQL has it built in Quote Link to comment Share on other sites More sharing options...
.josh Posted August 27, 2006 Share Posted August 27, 2006 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] Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 Ok im on the second prob know im trying to get the feild names of the table dynamicalyigot 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted August 27, 2006 Share Posted August 27, 2006 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 arraywhile ($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] Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 yes thanx alot i dint know about not embeding them, hay how cums people use echo instead of print?thanx for ur helpv greatfull Quote Link to comment Share on other sites More sharing options...
.josh Posted August 27, 2006 Share Posted August 27, 2006 differences between echo and printhttp://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/4099% of the time you can use them interchangeably, as most code does not require the use of the finer aspects of the 2. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted August 27, 2006 Author Share Posted August 27, 2006 thanx m8 il post back if i hav any further probs thanx alot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.