"pdo::exec or pdo::execute?" Code Answer

4

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

  1. 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 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) {}
    

    example:

    $myquery = "update users set email = 'testing'";
    $affectedrows = $db->exec($myquery);
    
  2. 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 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) {}
    

    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);
    
  3. 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 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) {}
    

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.

By The Coding Wombat on October 17 2022

Answers related to “pdo::exec or pdo::execute?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.