如何从表中获取用户名?

时间:2014-07-10 19:56:26

标签: php mysql

我想从表中获取用户名,但有些错误。

$hash = $_SESSION["hash"];

$con=mysqli_connect("127.0.0.1","root","","abc");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con, "SELECT username FROM members
WHERE password=" . $hash);

$row = mysqli_fetch_array($result);
$end = $row['username'];

错误
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\xx_code\members_01\memberpage.php on line 24

1 个答案:

答案 0 :(得分:1)

尝试在password-hash语句中添加引号和分号:

mysqli_query($con, "SELECT username FROM members WHERE password='" . $hash . "';");

还要确保使用的数据库中存在表members和列username

另请注意,您的代码容易受到SQL注入攻击(这是一个严重的问题......)。请参阅此处:How can I prevent SQL injection in PHP?