PDO::exec() or PDO::query()?

前端 未结 2 2003
时光说笑
时光说笑 2020-12-05 00:45

I used to have this as one of the options (4th param) passed to PDO constructor:

$aOptions[PDO::MYSQL_ATTR_INIT_COMMAND] = \"SET NAMES utf8\";
2条回答
  •  萌比男神i
    2020-12-05 01:04

    When using PDO::EXEC the result returned is not of an PDOStatement but an integer of the rows affected.

    When using PDO::QUERY the result returned is a PDOStatement.

    So the answer is it depends on what you need to do with the data, if you need to run query and not do anything with the results, then you should use exec to execute the query, otherwise if you need the number of rows, the data returned you should use pdo::query and then use the results returned by the call.

    • http://www.php.net/manual/en/pdo.query.php
    • http://php.net/manual/en/pdo.exec.php

    in regards to the bug there are several work around that you can take

    • Install PDO_MYSQL
    • Replace MYSQL_ATTR_INIT_COMMAND with 1002
    • Update your PHP To the latest stable release where it has been passed and patched.

    the second issue may have some issues on 64bit's OS's and Some windows configurations.

    Bug Information: http://bugs.php.net/bug.php?id=47224

提交回复
热议问题