gpt4 book ai didi

jquery - 生成包含两个数据值的 json 数组

转载 作者:行者123 更新时间:2023-11-30 00:31:41 25 4
gpt4 key购买 nike

请告诉我如何获取下面 json 数组上的值“内容”和“类别”的结果。目前它仅给出“内容”的结果

     $abc=$_GET["term"];

$query=mysql_query("
SELECT b.title as category ,a.title as content
FROM tb_content a, tb_categories b
WHERE ( b.title like '%".$abc."%'
OR a.title like '%".$abc."%')
AND a.categories_id=b.id
AND b.parent_id=81
");
$json=array();
while($display=mysql_fetch_array($query)){
$json[]=array(
'value'=> $display["content"],
'label'=>$display["content"],

);

最佳答案

给你:

  1. 在查询中使用变量之前,您始终应该转义变量。
  2. 您没有关闭 while 循环。
  3. 您对值和标签使用相同的键。
  4. 删除 while 循环中的最后一个逗号。
  5. 按照我的代码操作:

     $abc = mysql_real_escape_string( $_GET["term"] );

    $query = mysql_query("
    SELECT b.title as category ,a.title as content
    FROM tb_content a, tb_categories b
    WHERE ( b.title like '%".$abc."%'
    OR a.title like '%".$abc."%')
    AND a.categories_id=b.id
    AND b.parent_id=81
    ");
    $json = array();

    while ( $display = mysql_fetch_array($query) )
    {
    $json[]=array(
    'value' => $display["content"],
    'label' => $display["category"]
    );
    }

    //output:

    print_r($json); //as an array

    echo json_encode($json); // as a json string

关于jquery - 生成包含两个数据值的 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com