PHP - 我的SQL查询不能接受多个php变量

时间:2016-07-18 05:21:32

标签: php mysql select parameters boolean

下面是mysql查询,问题是只要只有一个php变量,例如

,查询才有效
 Select * From student where Account = ".$username."

但如果我将代码更改为如下所示,则会收到错误

  

警告:mysqli_num_rows()期望参数1为mysqli_result,给定布尔值。

这可能是什么问题,请帮助我。

    $result = $con->query("SELECT * FROM '.$role.' WHERE Account = '.$username.' AND password = '.$password.' AND IsApproved = '.$approval.'");

    if(mysqli_num_rows($result) == 1) {
      $_SESSION['username'] = $username;
      header('Location: include/test.php');
    }
    else {
      echo "Account is invalid";
    }
  }

编辑:问题已解决 - 由Dhaval Dave回答

 $result = $con->query("SELECT * FROM {$role} WHERE Account = '{$username}' AND password = '{$password}' AND IsApproved = '{$approval}'");

非常感谢大家帮助我提出所有建议,我很感激你们对我们的所作所为。非常感谢x1000

4 个答案:

答案 0 :(得分:-1)

$result = $con->query("SELECT * FROM ".$role." WHERE Account = ".$username." AND password = ".$password." AND IsApproved = ".$approval.");

答案 1 :(得分:-1)

请尝试

<?php
$result = $con->query('SELECT * FROM "'.$role.'" WHERE Account = "'.$username.'" AND password = "'.$password.'" AND IsApproved = "'.$approval'"');

答案 2 :(得分:-1)

在sql条件中传递字符串时,需要传递单引号。

$result = $con->query("Select * From student where Account = '".$username."' AND password = '".$password."' ");

答案 3 :(得分:-1)

试试这个,

$role = trim($role);
$result = $con->query("SELECT * FROM {$role} WHERE Account = '{$username}' AND password = '{$password}' AND IsApproved = '{$approval}'");