これが私のSQLリクエストです:
$sql
= 'CREATE TEMPORARY TABLE tmp '
. 'SELECT * FROM '.$table.' '
. 'WHERE id=:id; '
. 'ALTER TABLE tmp drop ID; '
. 'INSERT INTO '.$table.' '
. 'SELECT 0,tmp.* FROM tmp; '
. 'SET @last=LAST_INSERT_ID(); '
. 'DROP TABLE tmp;'
. 'SELECT @last; ';
$stmt = $this->bd->execQuery($sql, array(':id'=>101));
echo "1 -> = "; var_export($stmt); echo "\n";
$stmt = $stmt->fetch(PDO::FETCH_OBJ);
echo "2 -> = "; var_export($stmt); echo "\n";
ダンプトーク自体:クエリは機能します(私はチェックしました)。
sql =
'CREATE TEMPORARY TABLE tmp SELECT * FROM categorie WHERE id=:id; ALTER TABLE tmp drop ID; INSERT INTO categorie SELECT 0,tmp.* FROM tmp; SET @last=LAST_INSERT_ID(); DROP TABLE tmp;SELECT @last; '
params = array (
':id' => 101,
)
1 -> = PDOStatement::__set_state(array(
'queryString' => 'CREATE TEMPORARY TABLE tmp SELECT * FROM categorie WHERE id=:id; ALTER TABLE tmp drop ID; INSERT INTO categorie SELECT 0,tmp.* FROM tmp; SET @last=LAST_INSERT_ID(); DROP TABLE tmp;SELECT @last; ',
))
2 -> = false
コンソール行で「手動」で実行すると、それも機能します(コードの行が長いのでごめんなさい)。
mysql> CREATE TEMPORARY TABLE tmp SELECT * FROM categorie WHERE id=101; ALTER TABLE tmp drop ID; INSERT INTO categorie SELECT 0,tmp.* FROM tmp; SET @last=LAST_INSERT_ID(); DROP TABLE tmp;SELECT @last;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+-------+
| @last |
+-------+
| 141 |
+-------+
1 row in set (0.00 sec)
mysql>
これが実行中の私のコードです。
public function execQuery($sql, $tab=array())
{
$stmt = self::$_pdo->prepare($sql);
if ($stmt===false) {
throw new Exception(
'Erreur prepare '.$sql.
' = '.var_export(self::$_pdo->errorInfo(), true)
);
}
foreach ($tab as $key=>$valeur) {
$stmt->bindValue($key, $valeur);
}
if ($stmt->execute()===false) {
throw new Exception(
"Erreur execution de la requete :\n\"".$sql."\"\n".
"Paramètres de la requete :\n\"".var_export($tab, true)."\"\n".
"Details de l'erreur : \n".var_export(self::$_pdo->errorInfo(), true)
);
}
return $stmt;
}
ワンショットで最後に挿入された値を取得するにはどうすればよいですか(=私が行ったことを機能させる)?