PHP 预处理语句找不到错误
PHP prepared statement cant't find mistake
您好,我需要一个功能来检查用户是否已经存在于我的数据库中。
我出于特定原因需要这个。
此时函数始终returns false。
有人看到我在代码中的错误了吗?
function checkForUserExists($username){
$mysqli = new mysqli("****", "****", "****", "****");
if($stmt = $mysqli->prepare("SELECT * FROM users WHERE ign = ?")){
$stmt->bind_param("s", $username);
}else{
}
$stmt->execute();
$stmt->bind_result($ign);
if($ign == $username){
return false;
}else{
return true;
}
$stmt->close();
}
更新
$mysqli = new mysqli("**", "*", "**", "**");
$username = "Felipebuy";
if( $username && $stmt = $mysqli->prepare("SELECT ign FROM `users` WHERE `ign` = ?")){
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($ign);
$stmt->close();
if($ign == $username){
echo "equals";
}else{
echo "does not equal eachoter";
}
}
它只回显了"does not equal eachoter"
您在声明中 select ALL ( * )。除非您的 table 只包含一列,否则这将输出多个值。如果您将该结果 ($ign) 与单个值 ($username) 进行比较,它很可能永远不会 return true。
稍作更正,抱歉延迟 - 不得不睡觉。
function checkForUserExists( $username=false ){
$mysqli = new mysqli("****", "****", "****", "****");
if( $username && $stmt = $mysqli->prepare("SELECT `ign` FROM `users` WHERE `ign` = ?")){
$stmt->bind_param( "s", $username );
$res=$stmt->execute();
if( $res ){
$stmt->bind_result( $ign );
$stmt->fetch();
$stmt->close();
$mysqli->close();
return $ign === $username;
}
}
return false;
}
$result=call_user_func('checkForUserExists','Felipebuy');
echo $result ? 'equals' : 'not equal to each other';
您好,我需要一个功能来检查用户是否已经存在于我的数据库中。 我出于特定原因需要这个。 此时函数始终returns false。 有人看到我在代码中的错误了吗?
function checkForUserExists($username){
$mysqli = new mysqli("****", "****", "****", "****");
if($stmt = $mysqli->prepare("SELECT * FROM users WHERE ign = ?")){
$stmt->bind_param("s", $username);
}else{
}
$stmt->execute();
$stmt->bind_result($ign);
if($ign == $username){
return false;
}else{
return true;
}
$stmt->close();
}
更新
$mysqli = new mysqli("**", "*", "**", "**");
$username = "Felipebuy";
if( $username && $stmt = $mysqli->prepare("SELECT ign FROM `users` WHERE `ign` = ?")){
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($ign);
$stmt->close();
if($ign == $username){
echo "equals";
}else{
echo "does not equal eachoter";
}
}
它只回显了"does not equal eachoter"
您在声明中 select ALL ( * )。除非您的 table 只包含一列,否则这将输出多个值。如果您将该结果 ($ign) 与单个值 ($username) 进行比较,它很可能永远不会 return true。
稍作更正,抱歉延迟 - 不得不睡觉。
function checkForUserExists( $username=false ){
$mysqli = new mysqli("****", "****", "****", "****");
if( $username && $stmt = $mysqli->prepare("SELECT `ign` FROM `users` WHERE `ign` = ?")){
$stmt->bind_param( "s", $username );
$res=$stmt->execute();
if( $res ){
$stmt->bind_result( $ign );
$stmt->fetch();
$stmt->close();
$mysqli->close();
return $ign === $username;
}
}
return false;
}
$result=call_user_func('checkForUserExists','Felipebuy');
echo $result ? 'equals' : 'not equal to each other';