我正在尝试查询一个表,然后根据 if 语句将信息插入到该表中。
到目前为止,我有以下内容,但我一直收到错误
"No database selected"
数据库配置文件
$username = 'access@new';
$password = 'access';
try {
$conn = new PDO('mysql:host=localhost;dbname=new_site_co_uk', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
查询页面
function checkUser($uid, $oauth_provider, $username,$email,$twitter_otoken,$twitter_otoken_secret)
{
$query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'") or die(mysql_error());
$result = mysql_fetch_array($query);
if (!empty($result)) {
# User is already present
} else {
#user not present. Insert a new Record
$query = mysql_query("INSERT INTO `users` (oauth_provider, oauth_uid, username,email,twitter_oauth_token,twitter_oauth_token_secret) VALUES ('$oauth_provider', $uid, '$username','$email','','')") or die(mysql_error());
$query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'");
$result = mysql_fetch_array($query);
return $result;
}
return $result;
}
PDO
$sth = $conn->prepare('SELECT * FROM users WHERE oauth_uid = :uid AND oauth_provider = :oauth_provider');
$sth->bindParam(':uid', $uid);
$sth->bindParam(':oauth_provider', $oauth_provider);
$sth->execute();