我已从php5.2升级到php5.3。在此之前我的代码工作正常。升级后我得到了错误"电子邮件已经在使用...."虽然电子邮件不存在于数据库中。请帮忙。
<?php
$request = trim(strtolower($_REQUEST['username']));
$query = "SELECT username FROM advt WHERE username = '$username';";
$result = mysql_query($query) or die(mysql_error());
$valid = 'true';
if (mysql_num_rows($result) != 0)
$valid= '"The email is already in use, please choose another"';
{
echo $valid;
}
?>
答案 0 :(得分:0)
这是PDO中的一个实现。它可能无法正常工作,因为在PHP 5.5中不推荐使用mysql函数。将dbname=test
替换为您的数据库名称,并创建$user
和$pass
变量以使用正确的权限访问它
$username = trim(strtolower($_REQUEST['username']));
try {
$user = 'root';
$pass = 'password';
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("SELECT username FROM advt WHERE username = :username LIMIT 1;"); //Limit this to one result
$stmt->bindParam(':username', $username); // bind the parameters to prevent sql injection
$stmt->execute();
$valid = 'true';
//returns an array of results
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
//If the array is empty, no results have been returned
if (count($result) != 0) {
$valid = '"The email is already in use, please choose another"';
}
$dbh = null;
echo $valid;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
答案 1 :(得分:-1)
$ query =&#34; SELECT username FROM advt WHERE username = &#39; {$用户名}&#39 ;;&#34 ;;