English 中文(简体)
为什么要执行?
原标题:Why is execute() required before fetchAll()

我有以下法典:

$clicks = $statement->prepare($query);
$offers->bindValue(1, $id, PDO::PARAM_INT);
$clicks->execute();
$results = $clicks->fetchAll();
  • The Query works fine
  • The binding works fine

What actually makes me wonder, why I have to first execute() the query to can call fetchAll(). I ve come to this solution/assumption since, execute() only returns true/false and without calling execute(), fetchAll() will not return anything. Is this the normal way of doing that?

我早就在很久以前就与裁军部合作,因此我可以非常记住如何妥善处理该问题。

非常感谢

(如果使用PHP5.3,上述代码为PDO/PDOStatement)

最佳回答

Well,SlectT* from myTable is only a part of text, 你必须首先将其寄给服务器,使其与服务器相平,然后才能上fetchAll()。 <代码>fetchAll.nt t natural execute(<>>/code> is that fetch 所有均可被两次点击,如果你只需要做一次点击的话,你不想重复两次:

它还涉及具有约束力的参数等。 参数可以改变,但你可能仍然希望利用电离层功能处理原始参数价值。

它是一个设计事项,使与项目设计处的方案拟定工作更加容易。 这只是要求增加一条法典:

你也许会创造出一个包裹,使<条码>fetchAll()既能执行,又能使你再次陷入困境。

edit: Another reason is that execute() actually takes an optional array of parameters which allows you to bind parameters. $stmt -> execute(array($id)); would allow you to skip the bindValue calls.

问题回答

PDO prepare() uses prepared statements, so yes: they must be prepared, then executed in order to have any results. You can use query() if you don t need to reuse the statement.

由于一旦变数约束就自动执行,你就能够约束多种变数。 因此,当你通过明确执行声明而具有约束力时,你必须告诉你。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...