从MySQL查询返回JSON响应 [英] Return JSON Response from a MySQL Query

查看:288
本文介绍了从MySQL查询返回JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的jQuery AJAX函数:

I have a jQuery AJAX function like this:

$.ajax({
    url: 'crud/clients.php',
    dataType: 'json',
    type: 'POST',
    data: { 
        id:rowID,
        clientID:$('#clientID').val(),
        clientName:$('#clientName').val()
    }
    success: function(data){ 
        alert(data); 
    }
});

然后我在clients.php文件中设置查询.最后,我用这一行执行查询:

I then setup my query in the clients.php file. Finally, I execute the query with this line:

mysql_query( $sql ) or die(mysql_error());

如何以JSON格式返回该查询的结果,以便我的AJAX函数可以捕获返回的所有内容?

How do I return the results of that query in JSON format so that my AJAX function can catch whatever is returned?

推荐答案

尝试一下:

$q = mysql_query( $sql ) or die(mysql_error());
$res = json_encode(mysql_fetch_assoc($q));
echo $res;

您还可以查看: jQuery.getJSON & jQuery.parseJSON

you can also take a look at : jQuery.getJSON & jQuery.parseJSON

这篇关于从MySQL查询返回JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆