0

I am changing MySQL to PDO in a pagination functionality, there is a function that get me the result of select query and then I am using

$result=$DB->Select($query);
$result->fetch(PDO::FETCH_OBJ, PDO::FETCH_ORI_ABS,$_POST['from']);} 

for retrieving data from a certain position, but it always gives all records. In MySQL I was using mysql_data_seek to use this.

Weafs.py
  • 22,731
  • 9
  • 56
  • 78
Rahul Gahlot
  • 61
  • 3
  • 9
  • Your query simply needs to be adjusted; sounds like you aren't using LIMIT / OFFSET. The accepted answer in this question covers it: http://stackoverflow.com/questions/20364349/multiple-pages-using-mysql-limit-offset – Tieson T. Aug 21 '14 at 05:07
  • I have read some where that it have to add scrolling for this so i use $this->conn->setAttribute(PDO::ATTR_CURSOR , PDO::CURSOR_SCROLL)); but it giving me an error – Rahul Gahlot Aug 21 '14 at 05:12
  • I'm not sure what you mean by that. LIMIT/OFFSET (SKIP/TAKE in MS SQL) is a basic SQL idiom. It doesn't require anything except for you to supply the number of items you want, and how many rows to skip. – Tieson T. Aug 21 '14 at 05:15
  • I can not use LIMIT /OFFSET because i have a library that already created for fetching data. i just want to change mysql_data_seek. i have a function that get a query when called and execute and return result so i just have to change in just mysql_data_seek function – Rahul Gahlot Aug 21 '14 at 11:11

1 Answers1

0

Finally i found that CURSOR_SCROLL is not working in MYSQL So I create my own code to fetch data from MYSQL that works like mysql_data_seek

Here is the code

$res = $DB->conn->prepare($query);
$res->execute();

$res->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL,$frmdata['from'] );

$countRows=1;

$arr=array();

$row=$res->fetchAll(PDO::FETCH_OBJ);

foreach($row as $key => $value)
{
   if($key >=$frmdata['from'])
   {
     $arry[]=$value;
   }

}


for($i=0; $i<count($arry) ;$i++)

{
    if(!isset($frmdata['record']) || (isset($frmdata['record']) && $frmdata['record']!='All') )
    {
            if($countRows <= $frmdata['to'])
            {
                    $arr[]=$arry[$i];
            } 
    }
    else
    {
            $arr[]=$arry[$i];
    }   
    $countRows++;


}

return $arr;

Its working properly.

deceze
  • 510,633
  • 85
  • 743
  • 889
Rahul Gahlot
  • 61
  • 3
  • 9