使用MySQL和PHP返回单个值

时间:2017-04-12 10:58:39

标签: php mysql html5 http phpmyadmin

我正在让用户输入他们的电子邮件和密码,并且我希望从表中回显他们的用户名。这样我就可以证明我已经从表中检索了一个值。

我知道我的查询适用于phpmyadmin,并且我的php中也检索了电子邮件和密码。我使用当前代码获得的错误是HTTP错误500:

mysql_connect($database,$username,$password);
@mysql_select_db($username) or die ("Unable to select database");

$email = mysql_escape_string ($_POST['email']);
$password = mysql_escape_string ($_POST['password']);
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_escape_string($email);
$password = mysql_escape_string($password);


$SQL = "SELECT Name FROM users WHERE BINARY Email = '$email' and BINARY password = '$password'";

$result = mysql_query($SQL) or die("Unable to Run Query");

$value = mysql_fetch_object($result) or die("Unable to Fetch Object");

echo "<h2>$value</h2>" or die("Unable to Echo");

3 个答案:

答案 0 :(得分:0)

将陈述更改为

$SQL = "SELECT Name FROM users WHERE BINARY Email = '".$email."' and BINARY 
        password = '".$password."'";

答案 1 :(得分:0)

首先不要使用mysql_ *,因为它们已被弃用。你可以这样做:

$result = mysql_query($SQL);
if( mysql_num_rows($result) > 0 )
{
    $data = mysql_fetch_assoc($result);
    // $data is an array, to return a single value, do it like $data['name']
}

答案 2 :(得分:0)

你应该放弃旧的mysql连接器并切换到MySQLi。

但是,您应该查看mysql_result()