Php 分解MySQL中的数据,并使用json_encode传递值

Php 分解MySQL中的数据,并使用json_encode传递值,php,mysql,ajax,explode,Php,Mysql,Ajax,Explode,我对爆炸和内爆真的很陌生。我想分解数据库中的数据,并将值输入数组,以便使用json_encode将其传递到HTML页面 这是我的PHP文件 <?php session_start(); include "config.php"; $zzz = array(); $pass = array(); $idacara=mysql_real_escape_string($_GET["id"]); $mysql = ("select kategori from acara where id_acar

我对爆炸和内爆真的很陌生。我想分解数据库中的数据,并将值输入数组,以便使用json_encode将其传递到HTML页面

这是我的PHP文件

<?php
session_start();
include "config.php";
$zzz = array();
$pass = array();
$idacara=mysql_real_escape_string($_GET["id"]);
$mysql = ("select kategori from acara where id_acara='$idacara'");
$result=mysql_query($mysql);
if (!empty($result))
{
    while ($row=mysql_fetch_array($result))
    {
        $temp = explode(",",$row['kategori']);
        $count = count($temp);
        for($i=0;$i<$count;$i++)
        {
            $zzz=$pass[$i];
        }
        $fetchkategori[] = array
        (
            'kategori' => $zzz
        );

    }
}

mysql_close($con);

header('Content-Type:application/json');
echo json_encode($fetchkategori);
?>

这是我的HTML文件中的Ajax

var arrKategori=new Array();
        $.ajax({
            url: host+'/skripsi3/phpmobile/kategori.php',
            data: { "id": getacara},
            dataType: 'json',
            success: function(data, status){
                $.each(data, function(i,item){ 
                    if (arrKategori.indexOf(item.idkat)<0)
                    {   
                        $("fieldset").append('<input type="radio" name="radiokategori" class="required" id="'+item.idkat+'" value="'+item.idkat+'" required><label for="'+item.idkat+'">'+item.kategori+'</label>').trigger("create");  
                        arrKategori.push(item.idkat);

                    }       
                });
            },
            error: function(){
                //output.text('There was an error loading the data.');
            }
        });
var arrKategori=new Array();
$.ajax({
url:host+'/skripsi3/phpmobile/kategori.php',
数据:{“id”:getacara},
数据类型:“json”,
成功:功能(数据、状态){
$.each(数据、函数(i、项){

如果(arrKategori.indexOf(item.idkat)好,首先,不要使用
mysql.*
函数,而是使用
mysqli.*
PDO函数

您必须以以下方式在第一个
if
块之外声明
$fetchkategori[]

$fetchkategori = array();
之后,在
while
循环中,按以下方式存储数组

$fetchkategori[] = array
    (
        'kategori' => $zzz
    );
你的代码会工作的

以下是使用
mysqli
的完整解决方案

config.php

 $mysqli = new mysqli('mysql_hostname', 'mysql_username', 'mysql_password', 'mysql_database');
PHP文件

        <?php
    session_start();
    include "config.php";
    $zzz = array();
    $pass = array();
    $fetchkategori = array();
    $idacara=$_GET["id"];
    //preparing query
    $stmt = $mysqli->prepare("select kategori from acara where id_acara=?");
    $stmt->bind_param('i', $idacara);
    $stmt->execute();
    $stmt->store_result();//add this line
    $stmt->bind_result($kategori);
    if ($stmt->num_rows>0)
    {
        while ($stmt->fetch())
        {
            $temp = $kategori;
            $count = count($temp);
            for($i=0;$i<$count;$i++)
            {
                $zzz=$pass[$i];
            }
            $fetchkategori[] = array
            (
                'kategori' => $zzz
            );

        }
    }

    mysqli_close($mysqli);

    header('Content-Type:application/json');
    echo json_encode($fetchkategori);
    ?>


您的问题是什么?Hey@aldrin27感谢您的快速响应,我的问题是如何将分解数据输入数组,以便使用json编码将其传递到我的HTML。在我的HTML中,该值将显示为单选按钮。它不起作用,出现错误消息
警告:mysqli_query()至少需要2个参数,其中1个参数在第9行的C:\xampp\htdocs\skripsi3\phpmobile\kategori.php中给出如果我使用mysql,一条错误消息通知:未定义的偏移量:通知中的0:未定义的偏移量:通知中的1:未定义的偏移量:第18行的C:\xampp\htdocs\skripsi3\phpmobile\kategori.php中的2pp\htdocs\skripsi3\phpmobile\kategori.php第18行
{“kategori”:null}嘿,谢谢你的回答,但不幸的是,它不起作用。警告:mysqli_stmt::bind_param():类型定义字符串中的元素数与第10行C:\xampp\htdocs\skripsi3\phpmobile\kategori.php中的绑定变量数不匹配