简体   繁体   English

在PDO中缺少mysql_ *的一些有用功能

[英]Lack of some useful functions of mysql_* in PDO

I'd like to have a MySQL query or table object, say one that takes PDO object as input and doing all the necessary stuff with the table. 我想拥有一个MySQL查询或表对象,比如说一个以PDO对象作为输入并对表进行所有必要处理的对象。

I was thinking that this object could implement some array interfaces, like Countable , ArrayAccess , Iterator so I could easily foreach the rows like it was a regular array. 我在想,这个对象可以实现一些阵列界面,比如CountableArrayAccessIterator ,所以我可以很容易foreach行就像是一个普通的数组。

In deprecated mysql_* functions there was (still is, yet) very useful function mysql_data_seek() , which helped to move row-by-row, so I could implement all Iterator methods. 在不推荐使用的mysql_*函数中,有(仍然是)非常有用的函数mysql_data_seek() ,它有助于逐行移动,因此我可以实现所有Iterator方法。 I could run one query at the beginning and keep the rows in some resource private field and serve if I needed to. 我可以在开始时运行一个查询,并将行保留在某个资源专用字段中,并在需要时提供服务。

The other functions (mostly informational, like mysql_list_tables() and similar) I can cover by sending proper query to the db and fetch the result, but how could I move to specific row? 我可以通过向数据库发送适当的查询并获取结果来覆盖其他功能(大多数是信息性的,例如mysql_list_tables()等),但是我如何才能移至特定行?

I thought that the last parameter of the fetch() method is the cursor position, but when I call 我以为fetch()方法的最后一个参数是光标位置,但是当我调用

$p = $database->query('SELECT * FROM tobjects');
$row = $p->fetch(PDO::FETCH_ASSOC, PDO::ATTR_CURSOR,$i);

it's always resulting the same row for any value of $i . 对于$i任何值,总是会导致同一行。

EDIT 编辑

Actually, it's not the same row, but the first row at the first call to fetch() , second row for second etc., like there was no cursor information. 实际上,它不是同一行,而是第一次调用fetch()时的第一行,第二次调用的第二行,依此类推,就像没有游标信息一样。 As suggested in answers I did this 正如答案中所建议的那样

$p = $database->prepare('SELECT * FROM tobjects',
                    array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$p->execute();
$row = $p->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $i);

but this works the same. 但这是一样的。

You are using PDO::ATTR_CURSOR wrong. 您使用PDO::ATTR_CURSOR错误。 What you need to do is request a scrollable cursor first by using PDO::prepare , and then you can move to any record you want in the result set: 您需要做的是首先使用PDO::prepare请求一个可滚动的光标,然后您可以移动到结果集中所需的任何记录:

$s = $database->prepare('SELECT * FROM tobjects',
                        [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
$s->execute();
$row = $s->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $i);

I don't find any of these functions useful for whatever real life scenario. 我发现这些功能均不适用于任何现实生活中的情况。

mysql_list_tables() indeed can be substituted with WAY more informative and flexible query to INFORMATION SCHEMA mysql_list_tables()确实可以用更丰富,更灵活的方式查询INFORMATION SCHEMA来代替

Why bother with implementing ArrayAccess if you can have a regular array already? 如果已经可以使用常规数组,为什么还要麻烦实现ArrayAccess? There are only 2 scenarios: 只有两种情况:

  • either you need relatively small amount of data, fits for the web page. 您只需要相对少量的数据,就适合该网页。 Just select it all at once. 只需一次选择所有。

     $data = $stmt->fetchAll(); 

    And you can traverse the resulting array whatever way you like. 您可以按照自己喜欢的方式遍历结果数组。

  • or you need to traverse long table in some sort of console service script. 或者您需要使用某种控制台服务脚本遍历长表。 You don't need traverse it back and forth then. 然后,您不需要来回遍历。 Just select and loop over. 只需选择并循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM