Jump to content

How to get next two records in PHP and MYSQL


Dharmender

Recommended Posts

Use something like:

 

SELECT * FROM 'tablename' ORDER BY 'dateadded' DESC LIMIT 3;

 

Also, your query won't be executed there. You need to use your variable "$sql" as the mysql query.

 

$sql = "Your Query";
$result = mysql_query($sql);

 

Then use your "$result" variable to echo the data.

Do what beney said up until the last point (which is wrong).

 

Use:

 

<?php
$sql = "query";
$result = mysql_query($sql);
// This is where beney is wrong
//Use:
$data = mysql_fetch_array($sql);

foreach($data as $row){
//continue with your code
}
?>

don't use a foreach loop for database results. this is especialy true when using mysql_fetch_array as it will provide double the results that are looking for.

The tried and tested method:

$sql = "SELECT id, name FROM supplierinfo LIMIT 3"; //your SQL changed to only get the 3 results
$result = mysql_query($sql) or die(mysql_error()); //execute the query
while($row = mysql_fetch_assoc($result)) //while there are records then step through each row and fetch data into an associative array called $row
{
//display record by using $row['name'] and/or $row['id']
}

don't use a foreach loop for database results. this is especialy true when using mysql_fetch_array as it will provide double the results that are looking for.

The tried and tested method:

$sql = "SELECT id, name FROM supplierinfo LIMIT 3"; //your SQL changed to only get the 3 results
$result = mysql_query($sql) or die(mysql_error()); //execute the query
while($row = mysql_fetch_assoc($result)) //while there are records then step through each row and fetch data into an associative array called $row
{
//display record by using $row['name'] and/or $row['id']
}

 

It only displays current row. I want next two rows also to be displayed next to it.

 

I have found the solution for it using mysql_data_seek($result,$i)

Like This

    $result=mysql_query($sql);
			   $j = mysql_num_rows($result) - 1;
                       echo $j;
                       for ($i=0;$i<=$j; )
                       {

                       if (mysql_data_seek($result, $i))
                       {
					    $row=mysql_fetch_assoc($result);
					    //echo $row
					  }
				 if (mysql_data_seek($result, ++$i))
                       {
					    $row=mysql_fetch_assoc($result);
					    //echo $row
					  }
					 if (mysql_data_seek($result, ++$i))
                       {
					    $row=mysql_fetch_assoc($result);
					    //echo $row
					  }
			 }

No, just... No.

 

Do it like Muddy_Funster posted, as that is what you want to do. If you are having problems with getting his solution to work as you intended, then please post your code and explain what the problem is.

Your "solution" is more similar to a Rube Goldberg device than a solution, and not something that you should ever contemplate using at all.

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.