固定的!问题是我不知道您必须初始化与 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();