9

I want use mysql_data_seek with PDO from google search I found that it should looks like this:

$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);

however it's not work, what I do wrong? this is my code:

$query = "SELECT name,age FROM users";
$q = $db->prepare($query);
$q->execute();

$q->setFetchMode(PDO::FETCH_ASSOC);
$arrayData = $q->fetchAll();

foreach ($arrayData as $row){

    echo $row['name'] ." ";
    echo $row['age'] ."<br>";
}

$result = $q->fetch(PDO::FETCH_OBJ,PDO::FETCH_ORI_ABS,4);
var_dump($result);

I just want get the 5th row in object form from the last run query. I don't want run this query again (as some guys told me) I just want the results from sql buffer.

the var_dump result is: bool(false)

any ideas?

EDIT:

thanks for your answers and sorry but maybe I don't explain myself as well. I like the trick with JSON but the point is that the 5th row is example. I just want use the result of the query from the buffer with PDO exactly as I did it with mysql_data_seek in regular mysql (change the cursor). is it possible? I like all the tricks but that not what I look for.

Dennis
  • 704
  • 1
  • 9
  • 25
  • 1
    http://www.php.net/manual/en/pdostatement.fetch.php#105277 – CBroe Mar 26 '13 at 12:43
  • 2
    why not use? $result = $arrayData[4]; – jerjer Mar 26 '13 at 12:48
  • 1
    2 reasons: 1. I want it in the object form, 2. what happends if I have a lot of rows and I don't want save them in variable (only row by row ) use while($row = $q->fetch())? – Dennis Mar 26 '13 at 12:55
  • 2
    Reason 2 is not a real reason! If you are experienced in programming then you will do something like this: `$c = 1;$saved=null; while($row = $q->fetch()){if($c==4){$saved = $row};$c++;somethingelse;}` – ITroubs Mar 26 '13 at 13:03
  • maybe you right.. but the the point is that it is only example. the question asks how use mysql_data_seek with PDO.. – Dennis Mar 26 '13 at 14:01
  • 1
    The point is that you **never** need to seek with PDO. There is always a better solution, clearer and easier than seek. – Your Common Sense Mar 26 '13 at 14:39
  • Anyway, this is a not a real question. Please, ask certain question on some business task, not on some tool of which you got to know from other question. Please read here: [What is the XY problem?](http://meta.stackexchange.com/a/66378) – Your Common Sense Mar 26 '13 at 14:50

4 Answers4

7

the PDO 'cursor' default is PDO::CURSOR_FWDONLY that means that cursor can't back to zero like it happens with mysql_data_seek to allow cursor back to zero it necessary define use 'scrollable cursor'

example:

$db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

before use it like this:

$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);
Dennis
  • 704
  • 1
  • 9
  • 25
  • 1
    Seeking doesn't work with mysql+PDO. [bug report](https://bugs.php.net/bug.php?id=63466) – mpen Apr 11 '17 at 01:03
3
$result = $arrayData[4];

is all you need.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

If you want the 5th row result you can do like this:

$result = json_decode(json_encode($arrayData[4]), FALSE);
var_dump($result);

or something like this:

$object = new stdClass();
foreach ($array as $key => $value)
{
    $object->$key = $value;
}

Just curious! why do you need the object form?

EDIT (this will give the object form of the 5th row):

$index = 0;
$fifthRow = new stdClass();
while($row = $q->fetch())
{
   if($index++==4)
        $fifthRow = json_decode(json_encode($row), FALSE);
}   
jerjer
  • 8,694
  • 30
  • 36
  • 1
    i absolutely don't see the sense in our first codepart and i think you missunderstood his "objectform" word... – ITroubs Mar 26 '13 at 13:06
  • 1
    What a genious. Now you stole my code, put it into your answer and just wrapped the $row with json_decode(json_encode($row))... – ITroubs Mar 26 '13 at 13:16
  • thanks for the answer but it's not what I look for.. look at the edit sector in my question please I explain my self better (I think). – Dennis Mar 26 '13 at 13:51
  • Sorry! ITroubs forgot to put your name. – jerjer Mar 26 '13 at 13:59
1

You could do it like this:

$c = 1;
$saved=null; 
while($row = $q->fetch()){
    if($c==4){
        $saved = clone $row;
    };
    $c++;
    somethingelse;
}

$saved will then contain the 4th element as an object with almost no extra overhead calculations.

ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • 1
    thanks for the answer but its not what I look for. I explain my self better, please check the edit sector in my question. – Dennis Mar 26 '13 at 13:53