>我需要在页面中显示从mysql检索的数据。我的代码是:
jquery:
$('#search').on('click', function() {
alert($('.search_word').val());
var data_to_php = $('.search_name:visible').val(); //take the text only from the visible search box
alert(data_to_php);
//////////////////////////////////////////////////////////////////////
//The following code will get the search list populated by //////////
//retrieving data from mysql table////////////////////////////////////
//////////////////////////////////////////////////////////////////////
$.getJSON('./search.php', 'keyword='+data_to_php, function(info) {
$.each(info, function() {
$('ul.search_list').append('<li class="search_data">hi</li>'); //this code is for the purpose of testing only
console.log(info);
});
});
}); //end #search.on.click
PHP代码是:
require(connect.php);
$keywords = $_GET['keyword'];
$query = "SELECT name FROM test ORDER BY name WHERE name LIKE '%".$_GET['keyword']."%'";
$result = mysqli_query($conn,$query); //connection variable
//$conn is from
//included file
//connect.php
$jsonData = array();
while($row = mysqli_fetch_array($result)){
$jsonData[] = $row;
}
echo json_encode($jsonData);
?>
在console.log
输出中,我得到:
GET ./search.php?keyword=s 500 内部服务器错误,空白 响应。如果 php 不是,也可以生成内部错误 500 工作正常。代码中也可能存在一些错误。
现在phpinfo
正确显示结果。
我如何知道 getjson 调用 php 脚本是否成功?
MySQL查询中存在错误,您不能在WHERE子句之前调用ORDER BY,修改查询,
SELECT name FROM test WHERE name LIKE '%".$_GET['keyword']."%' ORDER BY name
了解MySQL中正确的执行顺序的非常好的参考 - MySQL查询/子句执行顺序
此外,require函数中的文件名应括在引号内 - require('connect.php');