PDO :: exec还是PDO :: execute?

编程入门 行业动态 更新时间:2024-10-28 09:25:39
本文介绍了PDO :: exec还是PDO :: execute?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I use PDO to connect to my database and I don't know which method is better than the other one for UPDATE, DELETE and INSERT, PDO::exec or PDO::excute. Which should I use?

推荐答案

尽管这两种方法都有类似的命名(exec,execute),但它们都打算在不同的场景中使用:

Although both methods have a similar naming (exec,execute) they're meant to be used in different scenarios:

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

  • exec is used to get the number of affected rows. /** * (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 php/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);

  • 当您要传递要绑定到查询中的参数数组时,使用
  • execute.

  • execute is used when you want to pass an array of parameters to be bind in the query.

    /** * (PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)<br/> * Executes a prepared statement * @link php/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) {}

    示例(当然,这也可能是UPDATE或DELETE查询):

    Example (of course this might be an UPDATE or DELETE query as well):

    $myQuery = 'SELECT * FROM users WHERE username = :username'; $params = array(':username' => 'admin'); $db->query($myQuery)->execute($params);

  • query返回PDOStatement对象

  • query returns a PDOStatement object

    /** * (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 php/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文档,或者再次使用PHPStorm,您可以查看PDO.php类的源代码.

    For more information you can visit the PHP Docs or in case you're using PHPStorm you can go though the source code of the PDO.php class.

    更多推荐

    PDO :: exec还是PDO :: execute?

    本文发布于:2023-10-26 20:33:01,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1531272.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:PDO   exec   execute

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!