dogbiter Posted June 6, 2008 Share Posted June 6, 2008 have they broken loops between php and mysql in recent versions??? i have PHP Version 5.2.4 and mysql 5.0.51a and i'm trying to get a simple patch of code to display everything in any given table. i've gotten it to work befor but it was over a year ago and almost forgot everything about php and mysql LOL. so i'm trying to keep it simple. basically here is the code ..... function DB($db){ $query = "select * from $db"; $query1 = mysql_query($query) or die(mysql_error()); $query2 = mysql_fetch_array($query1) or die(mysql_error()); echo "<table border=3>"; for ($i=0;$i<=mysql_num_rows($query1);$i++){ $id = $i; echo "<tr>"; $query7 = mysql_query("select * from $db"); $query6 = mysql_fetch_array($query7); for ($d=0;$d<mysql_num_fields($query7);$d++){ $query3 = mysql_fetch_array($query7); echo "<td>".$query2[$d]."</td>"; } echo "</tr>"; } echo "</table>"; } i've gotten the loop to work but it just fills it with the same lines over and over again. sorry it a little jumbled but i've been poking at it for a couple hours now or more. i had it working but i added a where statement to make the numbers the same as an ID column in the table BUT then it only works with tables with ID columns in sequential order. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 6, 2008 Share Posted June 6, 2008 have they broken loops between php and mysql in recent versions??? "between" makes no sense... mysql doesn't have loops... and your use $db twice, so the queries are identical. Quote Link to comment Share on other sites More sharing options...
dogbiter Posted June 6, 2008 Author Share Posted June 6, 2008 yes there are a few that are identical and i noticed that when i set variables in different places they did different things. and i'm sorry about "between" it's 5 am and no sleep but have they changed the way you have do loops??? or is what i have supposed to work??? like i said i've gotten it to work in the past but i cannot figure it out and most of that is off of memory. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 6, 2008 Share Posted June 6, 2008 The code snippet is very confusing... 1) you named variables "query" that are really "results" 2) query3 is the same as query7, but you use query2, which is never defined... 3) query7 is the same as query1 No wonder you're getting strange things... GIGO. Quote Link to comment Share on other sites More sharing options...
dogbiter Posted June 6, 2008 Author Share Posted June 6, 2008 oh here we go function DB($db){ $query = "select * from $db"; $result1 = mysql_query($query) or die(mysql_error()); $result2 = mysql_fetch_array($result1) or die(mysql_error()); echo "<table border=3>"; for ($i=0;$i<=mysql_num_rows($resutl1);$i++){ $id = $i; echo "<tr>"; for ($d=0;$d<mysql_num_fields($result2);$d++){ echo "<td>".$result2[$d]."</td>"; } echo "</tr>"; } echo "</table>"; } ok that's simplified to what it was when i first started working on it Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 7, 2008 Share Posted June 7, 2008 Nothing has changed to do with loop between PHP or MySQL versions. It is actually to do with your logic which is incorrect. mysql_fetch_array does not return all results. Each time you call mysql_fetch_array it'll return one row from the result set. Your code cleaned up <?php function DB($db) { $query = "select * from $db"; $result = mysql_query($query) or die(mysql_error()); // start HTML Table echo '<table border="1" cellspacing="1" cellpadding="5">' . "\n <tr>\n"; // display field name as headings // get the number of fields in the table $f = mysql_num_fields($result); // loop through the number of fields in the table for($i = 0; $i < $f; $i++) { // display table heading echo ' <th>' . mysql_field_name($result, $i) . "</th>\n"; } echo " </tr>\n"; // close table row // now display the data from the table // mysql_fetch_rows returns an array while($row = mysql_fetch_row($result)) { // we are using implode to extract the data from the $row array into sperate table cells echo " <tr> <td>" . implode("</td>\n <td>", $row) . "</td>\n </tr>\n"; } echo '</table>'; // close the table } ?> Quote Link to comment Share on other sites More sharing options...
dogbiter Posted June 9, 2008 Author Share Posted June 9, 2008 thank you for the help I'll try that(i connot right now as i'm on windows *need to restart to linux) and "implode()" is new to me and i know that the "fetch_array" got rows but the reiteration was supposed to display each row. I've gotten it to work by starting the starting at $i=1 instead of 0 and using the ID column in a "where" section of the query but that only works if the table has an ID column and it's sequential. Quote Link to comment Share on other sites More sharing options...
dogbiter Posted June 9, 2008 Author Share Posted June 9, 2008 thank you very much though it will bug the crap outta me untill i figure out how i had that other way working i like using implode works great. i had never seen it is that new??? or have i just never seen it??? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 9, 2008 Share Posted June 9, 2008 implode is a standard PHP function which has been available since php4. Have a read of the manual on Implode. 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.