0

I am trying to get the password of a specified username, assign it to a PHP variable, and then print it. Here's the code for it -

$user = $_POST["uname"];
$pass = mysqli_query($con, "SELECT pass FROM users WHERE username = '$user'");                                              
$result = mysqli_fetch_array($pass);

$specific = $result['pass'];

echo $specific;

The problem is that nothing is being printed at all! Even no error. What do I do?

5
  • You aren't checking for errors though.. php.net/manual/en/mysqli.error.php You also should use prepared statements, this is open to SQL injections. Also hopefully the passwords are hashed.. Commented Jan 29, 2016 at 16:45
  • $result =mysqli_fetch_array($pass,MYSQLI_ASSOC); use this. this is to fetch the db value into array format. Commented Jan 29, 2016 at 16:46
  • I am just testing the code, this isn't gonna be used anywhere. Just checking the basics. Commented Jan 29, 2016 at 16:54
  • 1
    You should test with what you are going to use. Commented Jan 29, 2016 at 16:55
  • I found my mistake! Sorry for the trouble. Commented Jan 30, 2016 at 7:46

2 Answers 2

1
$pass = mysqli_query($con, "SELECT pass FROM users WHERE username = '$user'") or die(mysqli_error($con)); 

This will Show you the Correct Error in your query

Sign up to request clarification or add additional context in comments.

Comments

1

Escape your values and check for errors:

$user = mysqli_escape_string( $con, $_POST["uname"] );
$pass = mysqli_query( $con, "SELECT pass FROM users WHERE username = '$user'");

# Error checking
if( $pass === false ) {
    echo 'Error: ', mysqli_error( $con );
}

# Check for no user with that password
if( mysqli_num_rows( $pass ) == 0 ) {
    echo 'No user with that username.';
}

# Use as associate arary              
$result = mysqli_fetch_assoc($pass);

$specific = $result['pass'];

echo $specific;

edit: Added check for no results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.