从php获取mysqli_fetch_stmt创建json数组

时间:2017-02-19 21:47:51

标签: php mysql json

我正在尝试在我的php mysql脚本中创建一个json数组来返回我的应用程序。我的供应商没有安装mysqlnd所以我被迫使用绑定结果...下面是正确获取我的数据的代码,但我不知道如何修复数据以匹配json数组id需要。

这是php代码:

  $cnt = "";
    $empty=array();
$resultArray=array();
$row=array();

    $sql =
    "select
    value as column1,
    value2 as column2
            from setting where name = ?";

    error_log("\n sql: " . $sql ,3,"master.log");

    if ($stmt = mysqli_prepare($conn, $sql))
    {
            mysqli_stmt_bind_param($stmt,'s', $sqlvalue1);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            $cnt = mysqli_stmt_num_rows($stmt);

            error_log("\n cnt=? : " . $cnt ,3,"master.log");

            if ($cnt == 0)
            {
                    error_log("\n cnt=0 : " . $cnt ,3,"master.log");
                    echo json_encode(array('result'=>$empty));
                    mysqli_close($conn);
                    exit;
            }
            if ($cnt > 0)
            {
                    error_log("\n cnt>0 : " . $cnt ,3,"master.log");
                    $resultArray = array();
                    $tempArray = array();
                    mysqli_stmt_bind_result($stmt, $column1, $column2);

                    while (mysqli_stmt_fetch($stmt))
                    {
                            error_log("\n while_json 2: " . $column1 . " " . $column2 ,3,"master.log");
                            //$tempArray = $row;
                            //array_push($resultArray, $tempArray);
                    }
                    echo json_encode(array('result'=>$resultArray));
                    error_log("\n while_json 3: " . print_R($resultArray,TRUE) ,3,"master.log");
                    mysqli_close($conn);
                    exit;
            }
    }
    else
    {
            $resultArray = array(array("success" => "NO", "message" => mysqli_errno($conn) . " " . mysqli_error($conn) , "count" => 0));
            echo json_encode(array('success'=>$resultArray));
            exit;
    }

json数据应如下所示:(2行返回)

{ “结果”:[{ “列1”: “0”, “列2”: “VB0023220131”},{ “列1”: “5”, “列2”: “CE004342420131”}]}

那么我如何(在while循环中)创建上面的字符串。

感谢。 PS可能是多行所以

0 个答案:

没有答案