好好爱自己!

php pdo and pdostatement

 

I'm a php and mysql beginner, I'm currently self study PDO and confused some concepts:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$sql = "SELECT * FROM users";
$users = $dbh->query($sql);

1.What is the relationship between PDO class and PDOStatement class?

$dbh is the new object of class PDO, but why $users is the PDOStatement object? fetchAll() is the function inside class PDOStatement, but you can use it like this $users->fetchAll(), is $usersPDO or PDOStatement object?

2.Someone said $users is the cursor, once consumed, it won't rewind to the beginning of the resultset.

foreach ($users as $row) {
    print $row["name"] . "<br/>";
}

but why you can use it in a foreach statement? foreach provides a way to iterate over arrays. what is cursor actually? is cursor a pointer?

3.For the pdostatement class, the doc said:

PDOStatement implements Traversable { ... }

why this class implements Traversable interface? is it empty interface?

Thank you for help!

shareedit
 
    
3. The very sample you're citing shows Traversable as a link... to a page where the Traversableinterface is explained. – DCoder Mar 14 '13 at 5:13
    
oh, Traversable detect if a class is traversable using foreach. why PDOStatement class need to do this? – nut Mar 14 '13 at 5:16
    
... because it is convenient for users to iterate over a result set with a foreach? – DCoder Mar 14 '13 at 5:17
    
As for point 1, mysql_connect returns a connection, and mysql_query returns a result set. In the PDO world, PDO and PDOStatement have roughly the same relationship - the first one represents a connection and the second one represents a result set of a particular query. – DCoder Mar 14 '13 at 5:19

2 Answers

According to the documentation, the Traversable interface allows you to use the object into a foreach loop and it's only supposed to be used internally. Think of it as a convenient way of using the PDOStatement.

Basically, with PDO there is two ways to execute a query, one by using PDO::prepare() & PDOStatement::execute() and the other one by using PDO::query(). The later does prepare/execute in one call.

PDO::query() and PDO::execute() will not return the results on the other hand the PDOStatement object will allow you to specify the data you want to return. PDOStatement::fetchAll() will allow to define how you want to have your data organized.

It seems more complicated on first sight but it provides more flexibility.

shareedit
 
 

All that mess is called "syntax sugar" and intended to sweeten a developer's life, though in real it makes taste too sugary to the point of disgust.

So, there are two kinds of object's characteristics - natural and unnatural ones.
And all your confusion come from the latter.

In your place I'd just forget of them, using objects straight way.

1.What is the relationship between PDO class and PDOStatement class?

That's 2 different classes. They serve different purposes. Like in old mysql you had connection resource and result resource. You have only one connection/PDO instance, but there can be any number actual query results/stmt classes.

but why you can use it in a foreach statement?

That's the very syntax sugar I talked above. They just added such a possibility to the statement object.

posted @   立志做一个好的程序员  阅读(338)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示