使用PDO从SELECT进行MySQL INSERT [英] MySQL INSERT from a SELECT with PDO
问题描述
使用PHP PDO进行SELECT查询中的INSERT时,我有一个奇怪的行为.直接在MySQL中测试查询效果很好,我插入了我的行:
I have a strange behaviour using PHP PDO for a INSERT from a SELECT query. Testing the query directly in MySQL it works well, I get my row inserted :
INSERT INTO sessionid (enc_id, enc_pass, enc_date)
SELECT AES_ENCRYPT(username, 'aeskey'), AES_ENCRYPT(pwd, 'aeskey'),
DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = 'a_user_name';
但是使用PDO,我每个用户一次插入了一行(279行)....这是PHP:
But using PDO, I have one row per user inserted at once (279 rows) .... Here is the PHP :
$sql_enc = '
INSERT INTO sessionid (enc_id, enc_pass, enc_date)
(SELECT AES_ENCRYPT(username, :aeskey), AES_ENCRYPT(pwd, :aeskey), DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = :username)
';
$res_enc = $pdo->prepare($sql_enc);
$res_enc->bindParam(':aeskey', $aeskey);
$res_enc->bindParam(':username', $username);
$res_enc->bindParam(':pwd', $username);
$res_enc->execute();
$res_enc = null;
我想念什么?我几乎可以确定这没什么,但是不能让它插入那一行.
What am I missing? I'm almost sure it's nothing but can't make it insert that single row.
谢谢.
fabien.
推荐答案
这不是可能的问题,但是您在代码的密码字段中输入了用户名.在查询中,在此插入aeskey.这是我能发现的唯一区别.
Not that it is the probable problem, but you put a username in the password field in your code. In your query you insert the aeskey there. It is the only difference I can spot.
这篇关于使用PDO从SELECT进行MySQL INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!