EDIT: SOLVED, SEE BELOW But still dont know what did I do to make it work :)
OK, I am stuck now. I have table users
:
ID int PRIMARY AUTO_INCREMENT
EMAIL varchar(60)
NICK varchar(60)
//...
If I do:
<?php
$email = $_POST["mai"];
$nickname = $_POST["nck"];
$mysqli = new mysqli($db_host, $db_username, $db_password, $database);
$prepared_statement = "INSERT INTO users VALUES(?,?,?)";
if ($stmt = $mysqli->prepare($prepared_statement)) {
$id = "";
$stmt->bind_param("iss",$id,$email,$nickname);
$stmt->execute();
}
?>
It works every single time. But if I do the same with select:
<?php
$previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
$check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=?;");
$check->bind_param("s",$email);
$check->execute();
$check->bind_result($maybe_id,$maybe_got_something);
while ($check->fetch()) { //here was typo, but fixed now
if ($maybe_got_something==$nickname){
echo "Hooray!";
}
}
?>
I never ever see the "Hooray!"
But, If I change it like this:
$previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
$prepared_statement = "SELECT ID,NICK FROM users WHERE EMAIL=";
$prepared_statement .=$email;
$result = $previous_entries->query($prepared_statement);
while ($row = $result->fetch_array()){
if ($row["NICK"]==$nickname){
echo "Hooray!";
}
}
Then everything is ok.
I am doing some terrible mistake in prepared statement. But I really really cannot find it... What am I doing wrong here?
EDIT: Updated the script to correct bad typo, and added these two rows:
echo "maybeid: ".. $maybe_id;
echo "maybenick:". $maybe_got_something;
Page echos this:
maybeid: maybenick:
EDIT: WORKING CODE When trying to debug it, I got to this:
$previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
$check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=?;");
$check->bind_param("s",$email);
$check->execute();
$check->bind_result($maybeid,$maybenick);
echo "maybeid: ".$maybeid;
echo "maybenick:". $maybenick;
// got rid of the if statement
while ($check->fetch()) {
echo "maybeid: ".$maybeid;
echo "maybenick:". $maybenick;
if ($maybenick==$nickname){
echo "Hooray!";
}
}
But ... why is it worrking? :)