【发布时间】:2014-08-09 02:35:06
【问题描述】:
我有一个 HTML 文本区域,每行包含 100-1000 个用户:密码对,我现在想使用 PDO 将它们插入我的数据库中。由于我对 PDO 非常陌生,因此我需要您的帮助,也许您知道一些优化方法,以实现更快的插入或更简单的代码。
这可能是我的 textarea 的内容:
User1:Pass1
User2:Pass2
User3:Pass3
这就是我尝试过的:
$query = "INSERT INTO Refs (username,password,targetLevel,idOrder) VALUES :accounts";
$ps_insert = $db->prepare($query);
//this iy my textarea from the form
$accounts = $_POST['accounts'];
// Convert Accountaray to user:password tuples
// Write the SQL Statement of the VALUE pairs into
// an string array in this style '(user,password)'
$msg = explode("\n", $accounts);
for($i = 0; $i < sizeof($msg); $i++)
{
list($user, $pass) = explode(":", $msg[$i]);
if(!empty($user)){
$insert_parts[$i]="('" . $user . "','" . $pass . "',10,0)";
}
}
// Content of this string is: (user1,pass1,10,0), (user2,pass2,10,0), (user3,pass3,10,0)
$accountInserts = implode(',', $insert_parts);
$ps_insert->bindParam(':accounts', $insert_parts);
$ps_insert->execute();
之前我使用了“众所周知的”MySQL 查询,但我想使用 PDO,因为我会将它用于其他事情以及常见的准备语句。感谢您的帮助!
问题:MySQL 插入不起作用。我应该如何解决这个问题?对(速度/代码)优化有何建议?
【问题讨论】: