gpt4 book ai didi

php jquery 显示来自 mysql getjson 的数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:45 24 4
gpt4 key购买 nike

我需要在页面中显示从 mysql 检索到的数据。我的代码是:

j查询:

$('#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 Internal Server Error with blank response. Internal error 500 can as well be generated if php is not working properly. There might also be some error in the code.

现在 phpinfo 可以正确显示结果。

如何知道对 php 脚本的 getjson 调用是否成功?

最佳答案

你的MySQL查询有错误,你不能在WHERE子句之前调用ORDER BY,修改你的查询,

SELECT name FROM test WHERE name LIKE '%".$_GET['keyword']."%' ORDER BY name

了解 MySQL 中正确执行顺序的非常好的引用 - MySQL query / clause execution order

此外,您在 require 函数中的文件名应该用引号引起来 - require('connect.php');

关于php jquery 显示来自 mysql getjson 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35659235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com