PHP如果语句mysqli_num_rows不起作用

时间:2015-03-02 09:18:27

标签: php mysql mysqli

我有一个代码,询问数据库中是否存在行,如果存在,则会回显消息。然而,无论我输入什么输入,都没有回应。

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
</head>
<body>

  <?php

    $mysqlhost="host";
    $mysqldatabase="b33_15887129_Accounts";
    $mysqlusername="username";
    $mysqlpassword="password";
    $connect=new mysqli($mysqlhost,$mysqlusername,$mysqlpassword);

    if (!$connect) {
      die("Connection failed: " . $connect->connect_error);
    }

    echo "Connected Successfully";

    mysqli_select_db($connect,"b33_15887129_Accounts");
    $loginusername=$_POST['loginusername'];
    $loginpassword=$_POST['loginpassword'];
    $checkifexist="SELECT * FROM Users WHERE Username='$loginusername' AND 'Password=$loginpassword'";
    $runquery=mysqli_query($connect,$checkifexist);
    $numofrows=mysqli_num_rows($runquery);

    if($numofrows==1){
      echo "Successfully logged in!";
    }else{
      echo "Failed to log in";
    }

  ?>
</body>
</html>

它设法回显Connected successfully,但无论输入是什么,Successfully logged inFailed to log in都不会出现。有什么帮助吗?

2 个答案:

答案 0 :(得分:2)

您的“登录失败”未显示,因为您忘记了回音。

if($numofrows==1){
    echo "Successfully logged in!";
}
else{
    echo "Failed to log in";

}

为了让sql工作,@ Randjith是正确的,你需要改变你的sql语句中的引用位置。

答案 1 :(得分:0)

1)你的其他声明没有回音!

2)你的SQL语句错了!检查密码的引号! - &GT;密码=&#39; $登录密码&#39;

实际上你应该看看http://php.net/manual/en/mysqli.real-escape-string.php