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!

aaarticlea/svg+xml,%3Csvg%20viewBox%3D%2237.625%2019.625%20116.75%20152.75%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2218%22%20height%3D%2224%22%3E%3Cg%20transform%3D%22%20translate(37.24881285659949%2018.506102847661907)%20scale(0.9611646158429531)%22%3E%3Cdefs%3E%3Cstyle%3E.cls-1%7Bfill%3A%23222260%3B%7D.cls-2%7Bfill%3A%23090910%3B%7D.cls-3%7Bfill%3A%23c09c71%3B%7D.cls-4%7Bfill%3A%23fff%3Bstroke%3A%23bcbec0%3Bstroke-miterlimit%3A10%3Bstroke-width%3A2px%3B%7D.cls-5%7Bfill%3A%23ead6b5%3B%7D.cls-6%7Bfill%3A%23d5ba93%3B%7D.cls-7%7Bfill%3A%23c1b030%3B%7D%3C%2Fstyle%3E%3C%2Fdefs%3E%3Ctitle%3E-%3C%2Ftitle%3E%3Cpath%20class%3D%22cls-1%22%20d%3D%22M15.09%2C100v17.25s9.2%2C2.62%2C50.33%2C0c46.48-3%2C46.39-4%2C46.39-4V88.88s-35.27%2C3.85-51.13%2C5.24S35.45%2C95.35%2C15.09%2C100Z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-2%22%20d%3D%22M81.42%2C85.64c-19.61-1.29-32.83-5-34.6-5.48C41.4%2C81.81%2C36%2C84%2C34.75%2C86.41c-.46%2C6%2C27.26%2C7.7%2C27.26%2C7.7l51.14-5.24S102.52%2C87%2C81.42%2C85.64Z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-1%22%20d%3D%22M70.16%2C65.77c-29.27%2C2.16-35.75%2C8.93-35.75%2C8.93l.21%2C11.71c1.28-2.41%2C6.71-4.6%2C12.13-6.25C53.09%2C78.23%2C59%2C77%2C59%2C77c21.41-3.08%2C27.55-6.62%2C27.55-6.62l-.29-7.75C85%2C64.18%2C70.16%2C65.77%2C70.16%2C65.77Z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-2%22%20d%3D%22m58.75%2052.22s-3.65%205.9%204.36%209.19%2010.23%204.05%2010.23%204.05%2013.08-0.82%2012.93-2.82c0%200-27.98-2-27.52-10.42z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-3%22%20d%3D%22m56.25%2019.55v-14a4.18%204.18%200%200%201%204.17%20-4.17%204.18%204.18%200%200%201%204.17%204.17v14%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-4%22%20d%3D%22M106.3%2C153.38A239.69%2C239.69%2C0%2C0%2C0%2C20.38%2C158%2C9.76%2C9.76%2C0%2C0%2C1%2C8.7%2C151.35L2.55%2C129.4c-1.43-5%2C2.22-10.37%2C8.21-11.79a281.2%2C281.2%2C0%2C0%2C1%2C100.79-5.4c6.11.78%2C10.32%2C5.76%2C9.43%2C10.85l-3.77%2C22.48A9.76%2C9.76%2C0%2C0%2C1%2C106.3%2C153.38Z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-5%22%20d%3D%22M45.14%2C16.91c-2.7%2C0-4.6%2C1.21-4.6%2C3.91V41.23c0%2C1.74%2C0%2C3.35%2C1.55%2C4.22L59.21%2C55.82V16.91Z%22%3E%3C%2Fpath%3E%3Cpath%20class%3D%22cls-6%22%20d%3D%22M76%2C16.91H59.21V55.82L78%2C45.45c1.51-.87%2C3.3-2.48%2C3.3-4.22V20.82C81.27%2C18.13%2C78.72%2C16.91%2C76%2C16.91Z%22%3E%3C%2Fpath%3E%3Ccircle%20class%3D%22cls-7%22%20cx%3D%2225.97%22%20cy%3D%22136.6%22%20r%3D%224.61%22%3E%3C%2Fcircle%3E%3Ccircle%20class%3D%22cls-7%22%20cx%3D%2264.55%22%20cy%3D%22131%22%20r%3D%224.61%22%3E%3C%2Fcircle%3E%3Ccircle%20class%3D%22cls-7%22%20cx%3D%22100.6%22%20cy%3D%22133.6%22%20r%3D%224.61%22%3E%3C%2Fcircle%3E%3C%2Fg%3E%3C%2Fsvg%3E" alt="" width="18" height="24" />
NullPoiиteя

36.9k1689111
asked Mar 14 '13 at 5:10
nut

69341224
 
    
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.

answered Mar 14 '13 at 5:32
Thomas Potaire

4,3852035
 
 

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.

php pdo and pdostatement的更多相关文章

  1. PDO、PDOStatement、PDOException

    最近在学PDO  比较详细的资料 出处:http://blog.csdn.net/hsst027/article/details/23682003 PDO中包含三个预定义的类,它们分别是PDO.PDO ...

  2. PDO和PDOStatement类常用方法

    PDO — PDO 类 PDO::beginTransaction — 启动一个事务 PDO::commit — 提交一个事务 PDO::__construct — 创建一个表示数据库连接的 PDO ...

  3. PDO和消息队列的一点个人理解

    什么是消息队列,百度百科说,···消息队列····是在消息的传输过程中保存消息的容器. 看着网上林林总总的文章,都说是为了应对高并发,处理数据量超级大的一种数据容器,也可以说是利用各种方式,先把数据存 ...

  4. PHP PDO函数库详解

    PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,PDO更高效.目前而言,实现“ ...

  5. pdo简单操作

    PDO(PHP Data Object) 是PHP 5新出来的东西,在PHP 6都要出来的时候,PHP 6只默认使用PDO来处理数据库,将把所有的数据库扩展移到了PECL,那么默认就是没有了我们喜爱的 ...

  6. PHP中PDO的配置与说明

    住[PDO是啥] PDO是PHP5新加入的一个重大功能,因为在PHP5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接和处理,什么php_mysql.dll.php_pgsql.dll ...

  7. PHP5中PDO的简单使用

    PHP5中PDO的简单使用 标签: php数据库mysql扩展extensionexception 2012-05-06 10:27 27753人阅读 评论(0) 收藏 举报  分类: PHP(6)  ...

  8. PHP PDO函数库具体解释

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/565 PDO是一个"数据库訪问抽象层",作用是统一各种数据库的訪问接口 ...

  9. 使用PDO持久化连接

    无论是何种编程语言,几乎都要经常与各种数据库打交道.不过,众所周知的是,在程序与数据库之间建立连接是一件比较耗费资源的事情,因此编程技术领域的许多专家.前辈们就设想并提出了各种解决方案,以减少不必要的 ...

随机推荐

  1. 【Python】协程实现生产者消费者模型

    协程的实现为协作式而非抢占式的,这是和进程线程的最大区别.在Python中,利用yield和send可以很容易实现协程. 首先复习下生成器. 如果一个函数使用了yield语句,那么它就是一个生成器函数 ...

  2. 改造vim

    1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可: lingd@ubuntu:~/arm$sudo apt-get install vim vim-sc ...

  3. hdu 2087 剪花布条 kmp模板题

    也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复 ...

  4. WinForm ListView

    今天,我学习了公共控件中的ListView的内容. 首先,在利用ListView布置界面时,有以下三个方面: 1.视图:            在其右上方小箭头点击将视图改为Details:或者右键属 ...

  5. github本地库及clone常用命令

    新建本地库 1. cd d: 2. mkdir git 3. cd git 4. git init 5. git remote add  origin git@github.com:swportal/ ...

  6. java中|与||有什么区别?那么&与&&呢

    ||当左边为真时,就不运行右边的表达式了|当左边为真,还是会运算右边的表达式&&当左边为假时,就不会运算右边的表达式&当左边为假时,还是会运算右边的表达式

  7. C#中值类型与引用类型通俗理解

    关于值类型和引用类型已经有很多人写了很多文章,但是很多人也只是停留在字面上的理解,如果采用一种通俗的方法来解释,想必很多人都会理解.我们都知道值类型存储在栈上,引用类型存储在堆上,引用类型都是xxx类 ...

  8. LWP::UserAgent介绍2

    #这个LWP::UserAgent一般要配合其他模块使用 #比如: #HTTP::Request #HTTP::Cookie #HTTP::Respose #HTTP::Status #LWP::Us ...

  9. slf4j 之logback日志之sl4j架构【二】

    一.整体介绍 介绍: The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for v ...

  10. C#之委托(函数参数传递)【转】

    原文:http://blog.csdn.net/wangdan199112/article/details/18796527 在学委托这块儿的时候,函数参数这块不是很理解,于是针对一个例子做了深入的理 ...