-3

I want to get a username from a table, but something is wrong.

$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'];

Error
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

qadenza
  • 8,611
  • 18
  • 61
  • 108

1 Answers1

1

Try adding quotes and a semi-colon to your password-hash statement:

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

Also make sure that the table members and the column username exists in the used database.

Also note that your code is vulnerable to SQL-injections (which is a serious issue...). Refer here: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Max
  • 914
  • 9
  • 27