douan0729 2009-12-02 02:17
浏览 94
已采纳

如何将此postgres db查询的结果转换为json以返回到我的jquery调用?

I'm creating a contest sign-up page for our annual math competition at my school. It requires some AJAX like behavior when you click and select an item in a dropdown list.

I've gotten an event to fire when I select something in the dropdown (I'm currently showing an alert box):

<script type="text/javascript">
    $(function() {
        $("#student").change(onStudentChange);
     });

     function onStudentChange()
     {
         alert("Dropdown changed");
     }
</script>

What this needs to do, is do an asynchronous call to the server to get a list of contests that student is currently registered for.

I know I need to do a jquery ajax call. So I think my onStudentChange() function would look like this:

$.ajax({
    type : 'POST',
    url : 'get_registered_events.php',
    dataType : 'json',
    data: {
    studentid : $('#student').val()
    },
    success : function(data){
        // Do something once we get the data
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
        // Display error
        }
        });

        return false;
});

So I went ahead and created the get_registered_events.php, which I want to return the events the student is registered for.

My problem is, I'm not experienced with PHP and I'm having difficulty figuring out how I would return the data the database gave me to this ajax call in the JSON format. Another snag I have is our school is using a very old version of PHP so I have to use this PEAR JSON library.

Here is that PHP file I'm having trouble with:

<?php

if (!empty($_POST['studentid')) {

    include_once('JSON.php');
    $json = new Services_JSON();

    $dbconn = pg_connect("host=somehost dbname=somedb user=someuser password=somepassword") or die('Could not connect: ' . pg_last_error());
    $query = 'SELECT contest.id, contest.title, FROM contest, student_contest WHERE student_id = '.$_POST['studentid'].' AND contest.id = contest_id';
    $contests = pg_query($query) or die('Query failed: ' . pg_last_error());

    // Here I need to convert the rows of $contests (contest.id, contest.title), into JSON and return it to that ajax call.

}

?>

So my question is, how do I convert $contests (its rows), into JSON (contest.id, contest.title), and return that back to the ajax call that will make it above.

If anyone could point me in the right direction I would really appreciate it.

  • 写回答

3条回答 默认 最新

  • doushi7314 2009-12-02 05:34
    关注
    $myarray = array()
    while ($row = pg_fetch_row($contests)) {
      $myarray[] = $row;
    }
    
    echo json_encode($myarray);
    

    I think this have to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大