Russia Posted January 29, 2011 Share Posted January 29, 2011 Hey guys, Im trying to post data on a page from multiple rows. Here is how it looks: <?php //FIRST ROW $result0 = mysql_query("SELECT * FROM test_mysql")or die(mysql_error()); $row0 = mysql_fetch_array($result0); //SECOND ROW $result1 = mysql_query("SELECT * FROM test_mysql LIMIT 1,1")or die(mysql_error()); $row1 = mysql_fetch_array($result1); //THIRD ROW $result2 = mysql_query("SELECT * FROM test_mysql LIMIT 2,2")or die(mysql_error()); $row2 = mysql_fetch_array($result2); ?> <?php echo $row0['name']; echo "<br>"; echo $row0['lastname']; echo "<br>"; echo $row0['email']; echo "<br>"; ?> ----- <?php echo "<br>"; echo $row1['name']; echo "<br>"; echo $row1['lastname']; echo "<br>"; echo $row1['email']; echo "<br>"; ?> ----- <?php echo "<br>"; echo $row2['name']; echo "<br>"; echo $row2['lastname']; echo "<br>"; echo $row2['email']; echo "<br>"; ?> As you can see, I am needing to make a new MYSQL_QUERY all the time because I have to change the limit on it so It shows data on another row. Is there any way to do this easier by putting the row I want to show inside the $row2['email']. I have about 6 rows and I need to show data on a page. This is how it looks. Can anyone show me a simple way of doing this? Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/ Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 $query = "SELECT * FROM test_mysql"; $result = mysql_query($query) or die(mysql_error()); while( $array = mysql_fetch_assoc($result) ) { echo $array['name']; echo "<br>"; echo $array['lastname']; echo "<br>"; echo $array['email']; echo "<br>"; } Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/#findComment-1166869 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 the thing is, I want to show each row seperatly not at the same time... I think the code you gave me will show all the rows at the same time... Is that right? Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/#findComment-1166870 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 Is there any way to add another bracket to the $row2['email'] so it looks like $row2['email']['1'] Then code it so it recognizes the second bracket in the variable as the row it should read from? Or there is no such thing? Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/#findComment-1166872 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 the thing is, I want to show each row seperatly not at the same time... I think the code you gave me will show all the rows at the same time... Is that right? Then all you need to do is store the returned results in an array, and read from it as you need it. $query = "SELECT * FROM test_mysql"; $result = mysql_query($query) or die(mysql_error()); $result_stack = array(); while( $array = mysql_fetch_assoc($result) ) { $result_stack[] = $array; } Results will be in multidimensional array elements starting with $result_stack[0]. If you print_r() the resulting array, the structure should be pretty clear. echo '<pre>'; print_r($result_stack); echo '</pre>'; Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/#findComment-1166873 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 Wait I dont get it... How would the code fully look like? Where would I set it so I can show a specific row? Link to comment https://forums.phpfreaks.com/topic/226026-simplifying-mysql_fetch_array-limit/#findComment-1166874 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.