jQuery AJAX调用数据库查询 [英] jQuery AJAX call to a database query

查看:427
本文介绍了jQuery AJAX调用数据库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有AJAX功能,

function admin_check_fn(type)
{

    //$("#notice_div").show();

    //var allform = $('form#all').serialize();

    $.ajax({
        type: "POST",
        //async: false,
        url: "<?php bloginfo('template_url'); ?>/profile/adminquery_check.php",
        data: { type: type },
        //data: 'code='+code+'&userid='+userid,
        dataType: "json",
        //dataType: "html",

        success: function(result){
            var allresult = result.res
            $('#result').html(  allresult  );

            alert(allresult);
            //$("#notice_div").hide();  
        }
    })
}

和服务器端:

$queryy="SELECT * FROM wp_users";
$name = array();    
$resultt=mysql_query($queryy) or die(mysql_error()); ?>

<?php while($rowss=mysql_fetch_array($resultt)){  

    $name = $rowss['display_name']; 

}

echo json_encode( array( 

    "res" =>  array($rowss['display_name']),
    "fvdfvv" => "sdfsd"

    ) 
);

基本上出于某种原因,它没有显示从查询到数据库中的用户表的所有返回值.当我查询仅包含一个条目的另一个表时,它可以工作,所以我认为这可能与以下事实有关:存在一个数组,该数组无法正确解析?

Basically for some reason it is not displaying all of the returned values from the query to the users table in the database. It works when I query another table with just one entry in it, so im thinking it could be something to do with the fact there is an array it is not parsing correctly?

只是想知道是否有人遇到过这个问题?

Just wondered if anyone else has came accross this problem?

谢谢

推荐答案

您的ajax成功将返回的数据视为html,但它是json.

Your ajax success is treating the returned data as html but it is json.

var allresult = result.res
 /* assumes allResult is html and can be inserted in DOM*/
 $('#result').html(  allresult  );

您需要解析json以创建html,或从服务器返回html

You need to parse the json to create the html, or return html from server

也是php循环:

$name = $rowss['display_name']; 

应该更像是:

$name[] = $rowss['display_name']; 

这篇关于jQuery AJAX调用数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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