如何在PDO中使用mysql_data_seek? [英] how use mysql_data_seek with PDO?
问题描述
我想将mysql_data_seek与来自Google搜索的PDO一起使用,我发现它看起来应该像这样:
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);
我只想从上一次运行的查询中以对象形式获得第5行.我不想再次运行此查询(就像有些人告诉我的那样),我只想要sql缓冲区的结果.
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.
var_dump结果为:bool(false)
the var_dump result is: bool(false)
有什么想法吗?
感谢您的回答,对不起,但也许我也无法解释自己.我喜欢JSON的技巧,但要点是第五行是示例.我只希望使用与PDO完全相同的缓冲区查询结果,就像我在常规mysql中使用mysql_data_seek所做的一样(更改游标).是否有可能?我喜欢所有的花招,但不是我想要的.
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.
推荐答案
PDO'cursor'的默认值为 PDO :: CURSOR_FWDONLY ,这意味着游标无法像mysql_data_seek那样回到零.要允许光标归零,必须定义可滚动光标"
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'
示例:
$db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
在像这样使用之前:
$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);
这篇关于如何在PDO中使用mysql_data_seek?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!