【问题标题】:json not giving back the proper result from database arrayjson 没有从数据库数组中返回正确的结果
【发布时间】:2011-09-02 01:53:38
【问题描述】:

我从查询中返回两行,我在 phpadmin 中对其进行了测试。

在萤火虫中我只能看到一行的数据。

有什么我看不到的问题?

$data = mysql_fetch_assoc($r);

        }
    }

    header('Content-type: application/json');           
    $output = array(
    "check" => $check,
    "users" => $data,
    "testnumberoffrows" => $number
    );

    echo json_encode($output);

在ajax函数中

if( data.check ){
    var user = data.users;
    console.log(user);

谢谢,理查德

【问题讨论】:

    标签: php jquery mysql json


    【解决方案1】:

    mysql_fetch_assoc() 仅获取 一个 行。你需要循环直到它返回FALSE,建立一个输出数组。

    类似这样的:

    while (($row = mysql_fetch_assoc($r)) !== FALSE) {
        $data[] = $row;
    }
    

    【讨论】:

    • 谢谢,我完全错过了,我让自己头疼
    【解决方案2】:

    请尝试

        $got=array();
        while ($row = mysql_fetch_array($r)) {
            array_push($got, $row);
        }
    
        mysql_free_result($r);
    
        header('Content-type: application/json');            
        $output = array( 
            "check" => $check, 
            "users" => $data, 
            "testnumberoffrows" => $number 
        ); 
    
        echo json_encode($output); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多