MySQL 使用 PDO 从 SELECT 插入
我使用 PHP PDO 从 SELECT 查询进行 INSERT 时有一个奇怪的行为。直接在 MySQL 中测试查询,效果很好,我插入了行:
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:
$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;
我缺少什么?我几乎可以肯定它没什么,但无法让它插入那一行。
谢谢。
法比恩。
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';
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.
Thank you.
fabien.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不是可能的问题,而是您在代码的密码字段中输入了用户名。在您的查询中,您在此处插入 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::bindParam 的 PHP 文档。那里的一位用户建议:
所以,您使用了 username 和 aeskey 两次。这样做:
Look at the PHP Documentation for PDO::bindParam. One user there suggested:
So, you used username and aeskey twice. Do it this way: