从带有 PDO 的 SELECT 中插入 MySQL

MySQL INSERT from a SELECT with PDO(从带有 PDO 的 SELECT 中插入 MySQL)
本文介绍了从带有 PDO 的 SELECT 中插入 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';

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.

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)