how use mysql_data_seek with PDO?

后端 未结 4 1681
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 22:57

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         


        
相关标签:
4条回答
  • 2021-01-04 23:47
    $result = $arrayData[4];
    

    is all you need.

    0 讨论(0)
  • 2021-01-04 23:49

    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);
    
    0 讨论(0)
  • 2021-01-04 23:50

    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.

    0 讨论(0)
  • 2021-01-04 23:51

    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);
    }   
    
    0 讨论(0)
提交回复
热议问题