dpfz27768 2015-08-30 11:32
浏览 54
已采纳

如何从mysql表中逐行获取数据,使用jquery(AJAX)在php中编码的查询?

I am a beginner in Ajax. I want to fetch data row from Subject Table consist of only one column Subject as varchar(100), defined in MySQL DB. Following is my php code.

Data.php

<?php

$con=mysqli_connect("","root","root","DBTemp") or die("</br> Error: " .mysqli_connect_error());

$sql="select * from Subject";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result))
 {
    echo $row["SUBJECT"];
    //I Want This Value to Be received in my Jquery Page
    //So that i can take certain action based on each Subject.
    //For example creating a select box child elements,options.
 }
?>

Jquery.js

$(document).ready(function()
 {
    var response='';
    $("body").ready(function()
     {
       $.ajax(
          {
            url: '/Data.php',
            type: 'GET'
            success: function(text)
                {
                     response=text;
                }
          });
     });
    $("body").append("<select> /*Get values here as options*/ </select>");
 });

But The Desired action is getting values row by row like:- 1st row value comes-> take certain action in jquery; 2nd row value comes-> take sertain action..; . . so on.

  • 写回答

3条回答 默认 最新

  • duansha8764 2015-08-30 12:07
    关注

    Data.php

    <?php
    
    $con=@mysqli_connect("","root","root","DBTemp");
    
    # Instead of that use header 500 to let javascript side know there is a real error.
    
    if (mysqli_connect_errno())
    {
        echo "Could not connect to database : ". mysqli_connect_error();
        header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
        exit();
    }
    
    
    $sql="select * from Subject";
    
    $result = mysqli_query($con,$sql);
    
    if (mysqli_error($con))
    {
        echo "Query failed : ".mysqli_error($con);
        header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
        exit();
    }
    
    $options = array();
    
    # populate options arrey with using table's id field as key 
    # and subject field as value.
    
    while($row = mysqli_fetch_assoc($result))
    {
        $options[$row['id']] = $row['subject'];
    
    }
    
    # return json encoded array to parse from javascript.
    echo json_encode($options);
    

    Data.php will output :

    {"1":"Subject ID 1","2":"Subject ID 3"}
    

    Jquery.js

    $(document).ready(function()
    {
    
        $("body").ready(function()
        {
            $.ajax(
                    {
                        url: '/Data.php',
                        type: 'GET',
                        dataType: 'json',  // Let jQuery know returned data is json.
                        success: function(result)
                        {
                            $.each(result, function(id, subject) {
                                # Loop through results and add an option to select box.
                                $("#ajaxpopulate").append( new Option(subject,id) )
                            });
    
                        }
                    });
        });
    
    });
    

    Page.html , inside the body. This select box will populated from ajax request.

     <select id="ajaxpopulate"></select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路