Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

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

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";

But just found that it does not work on certain php versions on Windows (does not work in 5.3) due to some bug.

Now I need to run SET NAMES utf8 using either $pdo->exec("SET NAMES utf8");

or $pdo->query("SET NAMES utf8");

right after the instantiating the pdo object. So, which one should I use - exec() or query()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...