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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题