0

I was wondering how to fetch data of a person from a table. I found the query to LIMIT data fetch from table. Here is what I got so far:

$result = mysql_query("SELECT * FROM users WHERE username = '" . $username[$x] . "' LIMIT " . $last_limt . " , " . $nxt_limt . "");

It returns data when LIMIT is available but if the LIMIT exceed the entire data returns null. So how can I know if ROW is available or not in table?

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
Nikhil
  • 911
  • 15
  • 28
  • When you reached to the limit and you still trying to fetch rows from table it not return any records as the limit of records you are fetching are not available. You can put a check while listing row data in php. – Parag Chaure Nov 05 '15 at 07:14
  • Come on. Stop with the deprecated API already! – Strawberry Nov 05 '15 at 07:39

2 Answers2

1

use mysql Count

SELECT count(username) FROM users WHERE username ='xyz'

And your $last_limt is not grater than total count-1

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
  • Can you be more specific please . $count = SELECT count(username) FROM users WHERE username ='xyz' if($last_limt<=$count-1){ $result = mysql_query("SELECT * FROM users WHERE username = '" . $username[$x] . "' LIMIT " . $last_limt . " , " . $nxt_limt . ""); } – Nikhil Nov 05 '15 at 07:10
0

You have to check number of rows of the result from that query before proceeding

$result = mysql_query("SELECT * FROM users WHERE username = '" . $username[$x] . "' LIMIT " . $last_limt . " , " . $nxt_limt . "");
$rowcount=mysql_num_rows($result);
if($rowcount > 0)
{
    //next operations

}
else
{
    //No more data
}
AkshayP
  • 2,141
  • 2
  • 18
  • 27
  • ya it was , But the problem was i used return null in else case . while any of query return null the entire result was returned as null . Now i solved that – Nikhil Nov 05 '15 at 08:38
  • In that case you can return empty array in else – AkshayP Nov 05 '15 at 09:51