当我添加变量[duplicate]时,我将数据转换为JSON的查询失败了

This question already has an answer here:

I am trying to get JSON from my database using PHP. This code works great in php:

$book = "PSA";
$chapter = "119";

$sql= "select * from bibledb_kjv where CHAPTERNO = $chapter;";
$link =mysqli_connect($host,$username,$password,$database);


$result= mysqli_query($link,$sql);

$response= array();

while($row=mysqli_fetch_array($result))
{



array_push($response,array("BOOKID"=>$row[2],"CHAPTERNO"=>$row[3],
"VERSENO"=>$row[4], "VERSETEXT"=>$row[6]));

}
echo json_encode(array("server_response"=>$response));
mysqli_close($link);


?>

example here: http://designlatitude.com/getversejson.php'

When I try to add the variable $book like this:

$book = "PSA";
$chapter = "119";

$sql= "select * from bibledb_kjv where BOOKID = $book and CHAPTERNO = 
$chapter;";
$link =mysqli_connect($host,$username,$password,$database);


$result= mysqli_query($link,$sql);

$response= array();

while($row=mysqli_fetch_array($result))
{


array_push($response,array("BOOKID"=>$row[2],"CHAPTERNO"=>$row[3],
"VERSENO"=>$row[4], "VERSETEXT"=>$row[6]));

}
echo json_encode(array("server_response"=>$response));
mysqli_close($link);

?>

I get {"server_response":[]} example here: http://designlatitude.com/getversejson2.php

When I run the queries in phpMyAdmin I get the same results each time with no errors.

What can I go to make this work? I did delete the connection part of the php for this question.

</div>

Your problem is how you insert the variable in the query string... The simple solution is to enclose it in quotes

$sql= "select * from bibledb_kjv where BOOKID = \"$book\" and CHAPTERNO = $chapter;";

The best solution is to use prepare statements http://php.net/manual/en/mysqli.prepare.php