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.

Link to comment
Share on other sites

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']
}

Link to comment
Share on other sites

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
					  }
			 }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.