我正在尝试查询表,然后根据if语句将信息插入到该表中。
到目前为止我有以下内容,但我一直收到错误
"No database selected"
DBConfig.php
$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();
答案 0 :(得分:2)
如果使用PDO连接到数据库,则必须使用PDO向数据库发送查询。在这种情况下,您无法使用mysql扩展名发送查询。
使用PDO::query()
发送SELECT
个查询,PDO::exec()
发送其他查询。您可能还想考虑prepared statements。