php查询总是假的,不管是什么

时间:2014-10-30 19:42:56

标签: php

我试图阅读并完成(合并)我在这里找到的任何内容。但我还没有找到解决方案。我使用wamp服务器,我有一个用户表,有2个用户,一个用电子邮件和密码作为test和test1,无论我尝试if语句总是返回false。

<?php

 $user = "root";
 $pass = "";
 $db = "testdb";

 $db = new mysqli("localhost", $user, $pass, $db) or die("did not work");

 echo "it connected";

 $email = "test";
 $pass1 = "test1";
 $qry = 'SELECT * FROM user WHERE email = " '. $email .' " AND password = " '.$pass1.' " ';

 $result = mysqli_query($db, $qry) or die(" did not query");

 $count = mysqli_num_rows($result);

 if( $count > 0)
     echo "        found user   ";
 else 
     echo "    did not find user or password";

 ?>

我试图增加mysqli_num_rows但是它总是真的

3 个答案:

答案 0 :(得分:1)

您的查询中有关于变量的空格:

" '. $email .' "

更改为:

"'. $email .'"

MySQL会在搜索匹配时按字面意思获取这些空格。

答案 1 :(得分:0)

我需要消除变量

封装中的空格
<?php

$user = "root";
$pass = "";
$db = "testdb";

$db = new mysqli("localhost", $user, $pass, $db) or die("did not work");

echo "it connected";

$email = "test";
$pass1 = "test1";
$qry = 'SELECT * FROM user WHERE email = "'. $email .'" AND password = "'.$pass1.'"';

$result = mysqli_query($db, $qry) or die(" did not query");

$count = mysqli_num_rows($result);

if( $count > 0)
    echo "        found user   ";
else 
    echo "    did not find user or password";

?>

答案 2 :(得分:0)

如果你使用的是mysqli类版本,那么你应该使用如下:

<?php

$user = "root";
$pass = "";
$db = "testdb";

$mysqli = new mysqli("localhost", $user, $pass, $db);

$email = "test";
$pass1 = "test1";

$qry = sprintf('SELECT * FROM user WHERE email = "%s" AND password = "%s"',$email,$pass1);
$result = $mysqli->query($qry);
$count = $result->num_rows;

 if( $count > 0)
     echo "        found user   ";
 else 
     echo "    did not find user or password";

$mysqli->close();
?>