Jump to content

Simplifying mysql_fetch_array limit


Russia

Recommended Posts

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.

 

2h8da3p.jpg

 

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

$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>";
}

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.