【问题标题】:PDO::exec or PDO::execute?PDO::exec 还是 PDO::execute?
【发布时间】:2014-11-10 17:07:42
【问题描述】:

我使用 PDO 连接到我的数据库,但对于 UPDATE、DELETE 和 INSERT、PDO::exec 或 PDO::excute,我不知道哪种方法比另一种更好。 我应该使用哪个?

【问题讨论】:

  • 它们非常不同。如果你需要像DELETE FROM tbl WHERE col = :value 这样的动态参数,你需要使用prepare()/bindParam()/execute()exec() 的目的是最简单的静态查询,例如 DELETE FROM tbl
  • 没有看到您对INSERT,UPDATE,DELETE 使用什么样的查询,我的猜测是您应该使用prepare()/execute(),因为大多数现代应用程序都必须根据输入的值执行这些语句用户。
  • 返回值有什么区别?如果Update,Insert,Delete没有错误,哪一个比较好?
  • 那是in the docs。如果您只需要知道有多少行受到影响,这就是exec() 返回的内容。但是对于 any 用户输入,建议您使用参数占位符 prepare()/execute()。然后你需要在执行的语句上调用$stmt->affectedRows()。另请参阅 this question 解决 exec()query() 之间的差异。。
  • 我投票决定将此问题作为题外话结束,因为它在问汽车或奶牛哪个更好。

标签: php pdo execute


【解决方案1】:

虽然这两种方法具有相似的命名(exec、execute),但它们旨在用于不同的场景:

  1. exec用于获取受影响的行数。

    /**
     * (PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)<br/>
     * Execute an SQL statement and return the number of affected rows
     * @link http://php.net/manual/en/pdo.exec.php
     * @param string $statement <p>
     * The SQL statement to prepare and execute.
     * </p>
     * <p>
     * Data inside the query should be properly escaped.
     * </p>
     * @return int <b>PDO::exec</b> returns the number of rows that were modified
     * or deleted by the SQL statement you issued. If no rows were affected,
     * <b>PDO::exec</b> returns 0.
     * </p>
     * This function may
     * return Boolean <b>FALSE</b>, but may also return a non-Boolean value which
     * evaluates to <b>FALSE</b>. Please read the section on Booleans for more
     * information. Use the ===
     * operator for testing the return value of this
     * function.
     * <p>
     * The following example incorrectly relies on the return value of
     * <b>PDO::exec</b>, wherein a statement that affected 0 rows
     * results in a call to <b>die</b>:
     * <code>
     * $db->exec() or die(print_r($db->errorInfo(), true));
     * </code>
     */
    public function exec ($statement) {}
    

    例子:

    $myQuery = "UPDATE users SET email = 'testing'";
    $affectedRows = $db->exec($myQuery);
    
  2. execute 用于传递要在查询中绑定的参数数组。

    /**
     * (PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)<br/>
     * Executes a prepared statement
     * @link http://php.net/manual/en/pdostatement.execute.php
     * @param array $input_parameters [optional] <p>
     * An array of values with as many elements as there are bound
     * parameters in the SQL statement being executed.
     * All values are treated as <b>PDO::PARAM_STR</b>.
     * </p>
     * <p>
     * You cannot bind multiple values to a single parameter; for example,
     * you cannot bind two values to a single named parameter in an IN()
     * clause.
     * </p>
     * <p>
     * You cannot bind more values than specified; if more keys exist in
     * <i>input_parameters</i> than in the SQL specified
     * in the <b>PDO::prepare</b>, then the statement will
     * fail and an error is emitted.
     * </p>
     * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
     */
    public function execute (array $input_parameters = null) {}
    

    示例(当然这也可能是UPDATEDELETE 查询):

    $myQuery = 'SELECT * FROM users WHERE username = :username';
    $params = array(':username' => 'admin');
    $db->query($myQuery)->execute($params);
    
  3. query 返回一个PDOStatement 对象

    /**
     * (PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.2.0)<br/>
     * Executes an SQL statement, returning a result set as a PDOStatement object
     * @link http://php.net/manual/en/pdo.query.php
     * @param string $statement <p>
     * The SQL statement to prepare and execute.
     * </p>
     * <p>
     * Data inside the query should be properly escaped.
     * </p>
     * @return PDOStatement <b>PDO::query</b> returns a PDOStatement object, or <b>FALSE</b>
     * on failure.
     */
    public function query ($statement) {}
    

有关更多信息,您可以访问PHP Docs,或者如果您使用的是PHPStorm,您可以查看PDO.php 类的源代码。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2018-09-13
    • 2019-07-20
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多