修复! 问题是我不知道你必须初始化与普通连接分开的PDO连接:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
我正在尝试学习PDO用于安全目的+只是良好实践。这是否正确我将如何将插入查询转换为PDO?它似乎没有工作。 $ dbh必须是我与服务器的连接正确吗?
$stmt = $dbh->prepare("INSERT INTO users (social_id, name, email, social_network, profile_pic) VALUES (:social_id, :name, :email, :social_network, :profile_pic)");
$stmt->bindParam(':social_id', $social_id);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':social_network', $social_network);
$stmt->bindParam(':profile_pic', $profile_pic);
// insert one row
$stmt->execute();
$user_id = $dbh->lastInsertId();
由此:
mysql_query("INSERT INTO users (social_id, name, email, social_network, profile_pic)
VALUES ('$social_id', '$name','$email', '$social_network','$profile_pic')");
$user_id = mysql_insert_id();
答案 0 :(得分:0)
问题是我不知道您必须初始化与普通连接分开的PDO连接:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);