简体   繁体   中英

Unable to get results from MySQL query in PHP

I'm unable to get results from a MySQL query in PHP. I've tried get_result() and bind_param() . However, this ends up breaking my script and I'm unable to echo any of the results for testing.

My table has three columns: username, password, and email.

$usernameprep = $conn->prepare("SELECT 1, password FROM `table` WHERE username=?");
$usernameprep->bind_param("s", $_POST['username']);
$usernameprep->execute();
$usernameprep->bind_results($userexists, $userpass);
$usernameprep->close();

What I've tried:

  • get_result() and bind_param()
  • putting bind_param() after AND before execute()
  • echoing $user and $userpass , only for my script to break afterwards
  • searching endless StackOverflow questions and none of them work for me

Note: I am used to SQLite3, so I'm still getting used to how MySQL works.

UPDATE: adding code for my connection for reference

$host = 'localhost';
$user = 'root';
$password = 'root';
$db = 'database';

$conn = new mysqli($host, $user, $password, $db);

// Check connection
if ($conn->connect_error) {
    echo "Connection Error, please try again.";
    die("Connection failed: " . $conn->connect_error);
} 

Updated

echo 'Here is your post data.  Make sure you have your username.<br>';
echo '<pre>';
print_r($_POST);
echo '</pre>';


$query = "SELECT password FROM `table` WHERE username=?";

$stmt = $conn->prepare($query);
$stmt->bind_param("s", $_POST['username']);
$stmt->execute();
echo("Error description: " . mysqli_error($conn)); //This will show you your errors after the execute();
$results = $stmt->get_result();

if($results->num_rows !== 0){

while($row = $results->fetch_assoc()){

  $data[] = array(

    'userexists' => 1,
    'userpass'  => $row['password']

  );

}

}else{

  echo "Your query failed to find any data.";

}

$stmt->close();

echo '<pre>';
echo 'Here is your data:<br>';
print_r($data);
echo '</pre>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM