PHP MySQL请求在使用变量时不起作用

时间:2016-04-10 07:28:22

标签: php mysql variables http-post

当我这样做时,MySQL请求正常工作:

$sql1 = 'SELECT * FROM user WHERE username LIKE "test"';

但是当我使用这样的变量时,它会出错:

$username = $_POST["username"];
$sql1 = 'SELECT * FROM user WHERE username LIKE'.$username;

错误:

  

致命错误:在布尔值...

上调用成员函数fetch_assoc()
var_dump的{​​p> $username给出了:

 string(4) "test"

感谢您的帮助!

到目前为止的完整代码:

$username = $_POST["username"];
var_dump($username);

include 'data.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql1 = 'SELECT * FROM user WHERE username = "'.$username.'"';
$result1 = $conn->query($sql1);
$row1 = $result1->fetch_assoc();

echo $row1["ID"];

1 个答案:

答案 0 :(得分:1)

感谢lakhvir kumar!我使用了两次变量名,也有一些语法错误。

更改了$ username,现在可以正常工作..