使用jquery从php文件获取数据的问题 [英] problem in getting data from php file using jquery
本文介绍了使用jquery从php文件获取数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我从php文件中获取JSON数据时遇到问题(PHP文件使用数据库即MySQL)。
代码如下所述。
jQuery
Hello guys,
I am getting a problem in getting JSON data from a php file (PHP file is using database i.e MySQL).
Code is mentioned below.
jQuery
$.getJSON("fetch.php",function(data){
$("ul").empty();
$.each(data.result,function()
{
$("ul").append("<li>Name : "+this['name']+"</li><br/> ");
$("ul").append("<li>Age : "+this['age']+"</li><br/> ");
$("ul").append("<li>Company : "+this['company']+"</li><br/> ");
});
});
PHP
PHP
<?php
include_once ('db.php');
$sql="Select * from people";
$res= mysql_query($sql);
$result=array();
while($row= mysql_fetch_array($res))
{
array_push($result, array('name' => $row[0],
'age'=>$row[1],
'company'=>$row[2]));
echo json_encode(array("result"=>$result));
}
?>
更新:问题是jquery没有在浏览器上提供任何输出。是的,我发布了查询输出的每一行,所以我在while循环中插入了echo。
Update : Problem is that jquery doesn't gives any output on the browser. Yes, I am posting every row of the query output so I inserted "echo" in while loop.
推荐答案
.getJSON( fetch.php, function (data){
.getJSON("fetch.php",function(data){
( ul)。empty();
("ul").empty();
.each(data.result, function ()
{
.each(data.result,function() {
这篇关于使用jquery从php文件获取数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文