teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

MDB2_PREPARE_MANIP

2020/07/28 02:24

投稿

thyda.eiqau
thyda.eiqau

スコア2982

answer CHANGED
@@ -4,4 +4,11 @@
4
4
  `$sql` にはSELECT文を入れているようですが、「// レコードの挿入」というコメントから察するに、途中でINSERT文に変わってないですか?(コードの途中を省略していませんか?)
5
5
  `$sth->execute()` はMDB2_Result型またはinteger型を返すようです。ドキュメントを見る限り、SELECT文だとMDB2_Resultが帰ってくるし、INSERT文 (とかUPDATEとか) だと影響を受けた行の数が返ってくるようです。
6
6
  - [Manual :: Prepare and execute SQL statements](https://pear.php.net/manual/en/package.database.mdb2.intro-execute.php)
7
- - [Docs For Class MDB2_Statement_Common #execute](https://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Statement_Common.html#methodexecute)
7
+ - [Docs For Class MDB2_Statement_Common #execute](https://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Statement_Common.html#methodexecute)
8
+
9
+ # 追記 Jul 28, 2020 11:23 AM
10
+ [MDB2のソースコード](https://github.com/pear/MDB2/blob/master/MDB2.php) を見ました。
11
+ 結論を言うとprepare()の第3引数に `MDB2_PREPARE_MANIP` を渡すと、影響を受けた行数が帰ってくるようです。
12
+
13
+ **というか先述のドキュメントにも書いてありました。**
14
+ > Since DML (data manipulation language - INSERT, UPDATE, DELETE) statements have different return values than data fetches the `prepare()` accepts a third parameter. This parameter should be set to **MDB2_PREPARE_MANIP** for DML statements (this way the number of affected rows will be returned). For data reads it should either be set to **MDB2_PREPARE_RESULT**, an array of data types for each of the columns in the result set or **NULL** in order to automatically detect the data types in the result set.