I've got a 'best practice' question about using PDO. I'm trying to finally get into object-oriented development and away from the PHP habits I developed ten years ago.
关于使用PDO,我有一个“最佳实践”问题。我正试图最终进入面向对象的开发,远离我十年前开发的PHP习惯。
My normal method of development was to open a DB connection at the beginning of a script/page, then do a bunch of mysql_query
calls as needed (mostly SELECT
and INSERT
). I'm using PDO for the first time, and it looks like the practice here is to create distinct PDO objects for each query/transaction. These seems like it would create multiple connections to the same DB during a string, which seems like a lot of unnecessary overhead.
我的常规开发方法是在脚本/页面的开头打开一个数据库连接,然后根据需要进行一堆mysql_query调用(主要是SELECT和INSERT)。我第一次使用PDO,看起来这里的做法是为每个查询/事务创建不同的PDO对象。这些似乎会在字符串期间创建到同一个DB的多个连接,这似乎是很多不必要的开销。
Is my read on this totally wrong?
我读完这个完全错了吗?
My apologies if this is covered somewhere I missed. I did look through *, php.net, and a PHP 5.3 book I have.
如果我错过了这个地方,我很抱歉。我确实通过*,php.net和我有的PHP 5.3书来查看。
3 个解决方案
#1
2
No, you should not create multiple instances of PDO in that case. Just create 1 instance and use PDO::query()
on it. For example:
不,在这种情况下,您不应该创建多个PDO实例。只需创建1个实例并在其上使用PDO :: query()。例如:
$pdo = new PDO(...);
/* ... */
$pdo->query("SELECT * FROM table1");
/* ... */
$pdo->query("SELECT * FROM table2");
/* ... etc ... */
If the query contains parameters, then prefer using PDO::prepare()
and PDOStatement::execute()
instead of PDO::query()
. You can find an example in the documentation for PDO::prepare()
.
如果查询包含参数,则首选使用PDO :: prepare()和PDOStatement :: execute()而不是PDO :: query()。您可以在PDO :: prepare()的文档中找到一个示例。
#2
2
The PDO Object stores the connection in which the queries are carried out. Normally, you only need one of those.
PDO对象存储执行查询的连接。通常,您只需要其中一个。
While there are cases where 2 connections might be convinient (When connecting to entirely different databases for instance), you generally only need one instance of the PDO class.
虽然有些情况下2个连接可能很方便(例如,当连接到完全不同的数据库时),通常只需要一个PDO类的实例。
What you will have multiple instances of, is the PDOStatement class. Which stores the query statements themselves (as well as the results). So for every query you will have a single PDOStatement instance.
你将拥有多个实例,是PDOStatement类。其中存储查询语句本身(以及结果)。因此,对于每个查询,您将拥有一个PDOStatement实例。
#3
0
yes, you initially connect to the database by creating an instance of the PDO object. But you use this object to run queries with and the results are packed into another class.
是的,您最初通过创建PDO对象的实例来连接到数据库。但是您使用此对象来运行查询,并将结果打包到另一个类中。
So you mainly have several classes with your query results, but just one connection to the database which runs the queries.
因此,您主要有几个包含查询结果的类,但只有一个连接到运行查询的数据库。
#1
2
No, you should not create multiple instances of PDO in that case. Just create 1 instance and use PDO::query()
on it. For example:
不,在这种情况下,您不应该创建多个PDO实例。只需创建1个实例并在其上使用PDO :: query()。例如:
$pdo = new PDO(...);
/* ... */
$pdo->query("SELECT * FROM table1");
/* ... */
$pdo->query("SELECT * FROM table2");
/* ... etc ... */
If the query contains parameters, then prefer using PDO::prepare()
and PDOStatement::execute()
instead of PDO::query()
. You can find an example in the documentation for PDO::prepare()
.
如果查询包含参数,则首选使用PDO :: prepare()和PDOStatement :: execute()而不是PDO :: query()。您可以在PDO :: prepare()的文档中找到一个示例。
#2
2
The PDO Object stores the connection in which the queries are carried out. Normally, you only need one of those.
PDO对象存储执行查询的连接。通常,您只需要其中一个。
While there are cases where 2 connections might be convinient (When connecting to entirely different databases for instance), you generally only need one instance of the PDO class.
虽然有些情况下2个连接可能很方便(例如,当连接到完全不同的数据库时),通常只需要一个PDO类的实例。
What you will have multiple instances of, is the PDOStatement class. Which stores the query statements themselves (as well as the results). So for every query you will have a single PDOStatement instance.
你将拥有多个实例,是PDOStatement类。其中存储查询语句本身(以及结果)。因此,对于每个查询,您将拥有一个PDOStatement实例。
#3
0
yes, you initially connect to the database by creating an instance of the PDO object. But you use this object to run queries with and the results are packed into another class.
是的,您最初通过创建PDO对象的实例来连接到数据库。但是您使用此对象来运行查询,并将结果打包到另一个类中。
So you mainly have several classes with your query results, but just one connection to the database which runs the queries.
因此,您主要有几个包含查询结果的类,但只有一个连接到运行查询的数据库。