编辑:解决,见下文但仍然不知道我做了什么让它工作:)
好吧,我现在被困住了。我有表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();
}
?>
每次都有效。但如果我选择同样的话:
<?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!";
}
}
然后一切都好。
我在准备好的声明中犯了一些可怕的错误。但是我真的找不到它......我在这里做错了什么?
编辑:更新了脚本以纠正错误的拼写错误,并添加了这两行:
echo "maybeid: ".. $maybe_id;
echo "maybenick:". $maybe_got_something;
Page echos this:
maybeid: maybenick:
编辑:工作代码 在尝试调试时,我得到了这个:
$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!";
}
}
但是......为什么这么令人担忧? :)
答案 0 :(得分:2)
准备好的查询为$check
,但您在提取时使用的是$stmt
。
答案 1 :(得分:1)
试试这个(使用从prepare返回的语句对象):
<?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!";
}
}
}
?>
答案 2 :(得分:1)
尝试使用此$stmt->fetch()
$check->fetch()
您可能需要将error_reporting级别设置为
error_reporting = E_ALL&amp; 〜E_NOTICE
在你的php.ini for development环境中避免这种错误。
此致
答案 3 :(得分:0)
您必须更改以下代码:
...
$check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=:email;");
$check->bind_param(":email",$email);
...