Подготовленный оператор работает для INSERT, но не для SELECT

EDIT: РЕШАЕМ, ПОСМОТРЕТЬ НИЖЕ Но не знаю, что я сделал, чтобы заставить его работать 🙂

Хорошо, я застрял сейчас. У меня есть users таблицы:

 ID int PRIMARY AUTO_INCREMENT EMAIL varchar(60) NICK varchar(60) //... 

Если я сделаю:

 <?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(); } ?> 

Он работает каждый раз. Но если я сделаю то же самое с 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!"; } } ?> по <?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!"; } } ?> работа <?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!"; } } ?> работа <?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!"; } } ?> 

Я никогда не видел «Ура!»

Но, если я изменю это так:

 $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!"; } } 

Тогда все в порядке.

Я делаю ужасную ошибку в подготовленном заявлении. Но я действительно не могу его найти … Что я здесь делаю неправильно?

EDIT: Обновлен скрипт для исправления плохой опечатки и добавил эти две строки:

 echo "maybeid: ".. $maybe_id; echo "maybenick:". $maybe_got_something; 

Страница echos:

  maybeid: maybenick: 

EDIT: WORKING CODE При попытке отладить его, я добрался до этого:

 $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!"; } } по $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!"; } } работа $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!"; } } работа $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!"; } } 

Но … почему это беспокоит? 🙂

Попробуйте это (используйте объект утверждения, возвращенный из подготовки):

 <?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); if($check->num_rows > 0){ while ($check->fetch()) { if ($maybe_got_something==$nickname){ echo "Hooray!"; } } } ?> по <?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); if($check->num_rows > 0){ while ($check->fetch()) { if ($maybe_got_something==$nickname){ echo "Hooray!"; } } } ?> работа <?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); if($check->num_rows > 0){ while ($check->fetch()) { if ($maybe_got_something==$nickname){ echo "Hooray!"; } } } ?> работа <?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); if($check->num_rows > 0){ while ($check->fetch()) { if ($maybe_got_something==$nickname){ echo "Hooray!"; } } } ?> 

Готовый запрос – $check но вы используете $stmt при извлечении.

Попробуйте изменить $stmt->fetch() с помощью этого $stmt->fetch() $check->fetch()

Вероятно, вам нужно установить уровень error_reporting на что-то вроде

error_reporting = E_ALL & ~ E_NOTICE

в вашем php.ini для разработки среды, чтобы избежать подобных ошибок.

С уважением

Вы должны изменить свой код следующим образом:

 ... $check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=:email;"); $check->bind_param(":email",$email); ... работа ... $check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=:email;"); $check->bind_param(":email",$email); ...