Php 为什么这个JSON不起作用?

Php 为什么这个JSON不起作用?,php,jquery,json,jquery-select2,Php,Jquery,Json,Jquery Select2,我想使用select2插件将MySQL远程数据用作标记。但是我对JSON的学习曲线非常弱。请帮助我更正此JSON格式 $("#e7").select2({ placeholder: "Search for a movie", minimumInputLength: 3, ajax: { url: "search.php", dataType: 'jsonp', quietMillis: 100, data:

我想使用select2插件将MySQL远程数据用作标记。但是我对JSON的学习曲线非常弱。请帮助我更正此JSON格式

$("#e7").select2({
    placeholder: "Search for a movie",
    minimumInputLength: 3,
    ajax: {
        url: "search.php",
        dataType: 'jsonp',
        quietMillis: 100,
        data: function (term, page) { // page is the one-based page number tracked by Select2
            return {
                q: term, //search term
                page_limit: 10, // page size
                page: page, // page number
                apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
            };
        },
        results: function (data, page) {
            var more = (page * 10) < data.total; // whether or not there are more results available

            // notice we return the value of more so Select2 knows if more results can be loaded
            return {
                results: data.movies,
                more: more
            };
        }
    },
    formatResult: movieFormatResult, // omitted for brevity, see the source of this page
    formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    escapeMarkup: function (m) {
        return m;
    } // we do not want to escape markup since we are displaying html in results
});
$(“#e7”)。选择2({
占位符:“搜索电影”,
最小输入长度:3,
阿贾克斯:{
url:“search.php”,
数据类型:“jsonp”,
安静百万:100,
数据:函数(术语,页码){//page是Select2跟踪的基于页码的函数
返回{
q:term,//搜索术语
页面限制:10,//页面大小
页码:第页,//页码
apikey:“ju6z9mjyajq2djue3gbvv26t”//请不要使用,这样这个示例就可以继续工作
};
},
结果:功能(数据、页面){
var more=(第*10页)
下面是search.php

<?php
    $sql=mysqli_query($db3->connection,"SELECT * FROM tags");

    while($row=mysqli_fetch_array($sql)){
        $tags=$row['tags'];
        $id=$row['id'];

        $response=array();
        $response['tags']=$tags;
        $response['id']=$id;     
    }

    echo json_encode($response);
?>

您的while循环没有填充$response数组$响应=数组();在循环的每次迭代中重置$response

$sql=mysqli_query($db3->connection,"SELECT * FROM tags");

$response=array();
while($row=mysqli_fetch_array($sql)){
    $tags=$row['tags'];
    $id=$row['id'];

    $response[]=array('tags'=>$tags, 'id'=>$id);

}

echo json_encode($response);

如果您不解释问题是什么,即您预期会发生什么、实际发生什么、您收到什么错误消息等,那么我们不太可能帮助您。请编辑您的问题并详细说明问题。不,“它不工作”不是一个有用的错误描述。我的烤面包机也不工作。请在while循环中重置$response。移动
$response=array()外部,并将内部替换为
$response[]=array('tags'=>$tags,'id'=>$id)