dennismonsewicz Posted September 10, 2009 Share Posted September 10, 2009 $q = mysql_query("SHOW TABLES FROM *****"); $row = mysql_fetch_object($q); foreach($row as $r) { print_r($r); } for some reason the code above is only returning the first record it finds and not every record.. the code above is to echo out ALL of the table column names Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/ Share on other sites More sharing options...
MatthewJ Posted September 10, 2009 Share Posted September 10, 2009 <?php $q = mysql_query("SHOW TABLES FROM ****"); $row = mysql_fetch_object($q); while($row = mysql_fetch_object($q)) { echo "<pre>"; print_r($row); echo "</pre>"; } ?> That will list all of the tables if that helps Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916149 Share on other sites More sharing options...
dennismonsewicz Posted September 10, 2009 Author Share Posted September 10, 2009 hmmm... that worked but how come you can't do it in a foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916152 Share on other sites More sharing options...
gr1zzly Posted September 10, 2009 Share Posted September 10, 2009 Hey, sorry to hijack your post but I'm having a similar problem and urgently need help. Posted here but no replies yet - link below: http://www.phpfreaks.com/forums/index.php/topic,268388.0.html Surely someone on here knows how to use foreach's with mysql results objects? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916154 Share on other sites More sharing options...
PFMaBiSmAd Posted September 10, 2009 Share Posted September 10, 2009 Each call to a mysql_fetch_xxxxx function, returns ONE ROW from the result set (please see the php.net documentation.) To iterate over all the rows in a result set, you must use a loop that continues until there are no more rows to fetch. The foreach() loop that was posted in this thread just iterates over the columns in one row. Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916162 Share on other sites More sharing options...
dennismonsewicz Posted September 10, 2009 Author Share Posted September 10, 2009 ok... so now that I have the while loop going how do I echo out just one of the tables? I can print_r($row) all day long, but how do I access just one of the DB tables? Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916164 Share on other sites More sharing options...
MatthewJ Posted September 10, 2009 Share Posted September 10, 2009 Not real familiar with mysql_fetch_object(), but this works <?php $q = mysql_query("SHOW TABLES FROM ****"); $row = mysql_fetch_assoc($q); while($row = mysql_fetch_assoc($q)) { echo $row['Tables_in_****']."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916210 Share on other sites More sharing options...
jcombs_31 Posted September 10, 2009 Share Posted September 10, 2009 ok... so now that I have the while loop going how do I echo out just one of the tables? I can print_r($row) all day long, but how do I access just one of the DB tables? That depends on your query and the method for retrieving the results. If you are using mysql_fetch_assoc, you would do something like while($row = mysql_fetch_assoc($result) { echo $row['fieldname']; } Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916216 Share on other sites More sharing options...
jcombs_31 Posted September 10, 2009 Share Posted September 10, 2009 What you are doing doesn't make sense. http://us.php.net/manual/en/function.mysql-fetch-assoc.php Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916221 Share on other sites More sharing options...
gr1zzly Posted September 10, 2009 Share Posted September 10, 2009 okay, please forgive my ignorance / stupidity but which part exactly? I literally have tried all kinds of different while and foreach loops and combinations so if the code I have now has got a bit lost somewhere I apologize. What I need is each whole row of results to be stored associatively in my $data array variable as one entry. So that each entry in $data is an associative array containing one row from my db. Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916238 Share on other sites More sharing options...
gr1zzly Posted September 11, 2009 Share Posted September 11, 2009 Ok I think I've got it working now. Sorry I wasn't fully understanding how to use the results with an array variable. I am now using the following: $r = @mysqli_query($dbc, $q); // OR $errors = mysqli_error($dbc); if ($r) { // If OK. $i = 0; while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) { $data[$i] = $row; $i++; }; return array(true, $data); } Using the extra $i var to reference the $data array allowed me to create an array entry for each row as a sub array containing all the rows cell data. Thanks to all your helps. Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-916613 Share on other sites More sharing options...
PrinceOfDragons Posted September 14, 2009 Share Posted September 14, 2009 I made a function for this type of thing. function name() { $sql = "SELECT * FROM table_name"; $results = mysql_query($sql, $dCon); $row = mysql_fetch_assoc($results); while($row = mysql_fetch_assoc($results)) { echo '<tr>'; //Begins Table Row foreach($row as $column=>value) { echo '<td>'; //Begins Table Column echo $value; echo '</td>'; //Ends Table Column } } echo '</tr>'; //Ends Table Row } It seems this is not grabbing the first row out of the database though :'( Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-917994 Share on other sites More sharing options...
Philip Posted September 14, 2009 Share Posted September 14, 2009 I made a function for this type of thing. function name() { $sql = "SELECT * FROM table_name"; $results = mysql_query($sql, $dCon); $row = mysql_fetch_assoc($results); while($row = mysql_fetch_assoc($results)) { echo '<tr>'; //Begins Table Row foreach($row as $column=>value) { echo '<td>'; //Begins Table Column echo $value; echo '</td>'; //Ends Table Column } } echo '</tr>'; //Ends Table Row } It seems this is not grabbing the first row out of the database though :'( Because you called the first row ($row = mysql_fetch_assoc($results) and then overwrote it (while($row = mysql_fetch_assoc($results))) Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-918009 Share on other sites More sharing options...
PrinceOfDragons Posted September 14, 2009 Share Posted September 14, 2009 Thank You ! Still learning the PHP. Ok I took out the first $row = mysql_fetch_assoc($results) all works 4.0 . Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-918030 Share on other sites More sharing options...
Philip Posted September 14, 2009 Share Posted September 14, 2009 If I take out the first $row = msyql_fetch_assoc it wont workDid you try it? Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-918034 Share on other sites More sharing options...
PrinceOfDragons Posted September 14, 2009 Share Posted September 14, 2009 Ya I got it. Something was wrong in the database :-\ Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-918036 Share on other sites More sharing options...
gr1zzly Posted September 15, 2009 Share Posted September 15, 2009 Sorry I made a mistake before in addressing the array, the $i is not required -> just use the square brackets to assign a row... while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) { $data[] = $row; }; Like so! Then when you come to address the variable later a simple foreach ($data AS $row) { // Something to do with the data... } in this foreach() the $row is the whole row from the database as an associative array (depending on the query and results fetch method of course) and so can be addressed as $row[column_name], where 'column_name' is the name of the column you want the contents of. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/173803-foreach-help/#findComment-918880 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.