Dharmender Posted January 10, 2013 Share Posted January 10, 2013 In Mysql query I want to display three records at time $sql="select id,name from supplierinfo"; $result=mysql_query($result); foreach($result as $row) { //display record here for current row // I want next two records here } Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/ Share on other sites More sharing options...
Beeeeney Posted January 10, 2013 Share Posted January 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404614 Share on other sites More sharing options...
White_Lily Posted January 10, 2013 Share Posted January 10, 2013 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404617 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2013 Share Posted January 10, 2013 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'] } Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404618 Share on other sites More sharing options...
thara Posted January 10, 2013 Share Posted January 10, 2013 In Mysql query I want to display three records at time Is actually OP finding a solution with LIMIT clause here? Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404621 Share on other sites More sharing options...
Beeeeney Posted January 10, 2013 Share Posted January 10, 2013 Is actually OP finding a solution with LIMIT clause here? Sure, if you take some time and allocate it to reading the last 3 very short posts in this thread, you'll see. Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404622 Share on other sites More sharing options...
Dharmender Posted January 10, 2013 Author Share Posted January 10, 2013 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404626 Share on other sites More sharing options...
Christian F. Posted January 10, 2013 Share Posted January 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404637 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2013 Share Posted January 10, 2013 If it's only showing a single row then you're doing it wrong. There was a reason I used the words "tried and tested". Quote Link to comment https://forums.phpfreaks.com/topic/272937-how-to-get-next-two-records-in-php-and-mysql/#findComment-1404638 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.