douguo7431 2017-02-19 21:47
浏览 73

从php获取mysqli_fetch_stmt创建json数组

I am trying to create a json array in my php mysql script to return to my application. My vendor does not have mysqlnd installed so I am forced to use bind result... below is code that gets my data correctly but I not sure how to fix the data to match the json array id need.

Here is the php code:

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

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

    error_log("
 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("
 cnt=? : " . $cnt ,3,"master.log");

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

                    while (mysqli_stmt_fetch($stmt))
                    {
                            error_log("
 while_json 2: " . $column1 . " " . $column2 ,3,"master.log");
                            //$tempArray = $row;
                            //array_push($resultArray, $tempArray);
                    }
                    echo json_encode(array('result'=>$resultArray));
                    error_log("
 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;
    }

The json data should look like: (2 Rows returned)

{"result":[{"column1":"0","column2":"VB0023220131"},{"column1":"5","column2":"CE004342420131"}]}

So how do I (in the while loop) create the string above.

thanks. PS the may be multiple row so the

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题