douduiti3040 2014-06-07 08:22
浏览 74
已采纳

php使用mysqli生成json数组

I am learning php/mysql for a website I am building. I had everything working but it was incredibly vulnerable and insecure with little error trapping. I have begun trying to secure my code and the first thing I am trying to do is better handle errors and implement prepared statements to avoid sql injection.

Unfortunately I have got stuck at my first hurdle, I originally created my json array like this:

$myArray=array();
$tempArray = array();

while ( $row = $results->fetch_assoc())
{   
    $tempArray[0] = $row['unix_timestamp(end_date)'];
    $tempArray[0] *= 1000;
    $tempArray[1] = $row['bid'];
    array_push($myArray, $tempArray);
}
echo json_encode ($myArray, JSON_NUMERIC_CHECK);

That worked greate producing a json I could then use with highcharts on my website.

My question is this, I have updated my code to include prepared statements utilising Prepare and bind_param which works (tested by simply echoing the result) however I am really struggling to get the errors into an array as above:

 while ( $row = $select_stmt->fetch())
{   
    printf("%s %s
", $col1, $col2);
    //$tempArray[0] = $col2;
    //$tempArray[0] *= 1000;
    //$tempArray[1] = $col1;
   //array_push($myArray, $tempArray); 
}
//echo json_encode ($tempArray, JSON_NUMERIC_CHECK);

The above prints the text out but whenever I try and use fetch_assoc are another alternative (i've done a lot of googling) I cannot get it to work. I always receive the same sorts of errors "Call to undefined method..." How do I replicate what I had working and use mysqli with bind_result to produce an array?

I would appreciate understanding exactly where I am going wrong as I believe I am missing a concept which is leading me down the wrong paths when looking for a solution.

EDIT - I have checked and I am using the mysqlnd driver

UPDATED Code - I think this is closer - utilising the get_result method instead of "bind_result" before calling fetch_assoc again.

$result = $select_stmt->get_result();
$myArray=array();
$tempArray = array();

 while ( $row = $result->fetch_assoc())
{
    $tempArray[0] = $row['end_date'];
    $tempArray[0] *= 1000;
    $tempArray[1] = $row['bid'];
    array_push($myArray, $tempArray);
}
echo json_encode ($tempArray, JSON_NUMERIC_CHECK);

Unfortunately it only retrieves the first value and not the subsequent 10's or 100's or 1000's of rows

SOLUTION - correct array in the json_encode and use of the get_result function

$result = $select_stmt->get_result();

$myArray=array();
$tempArray = array();

while ( $row = $result->fetch_assoc())
{
    foreach ($row as $r){
        $tempArray[0] = $row['end_date'];
        $tempArray[0] *= 1000;
        $tempArray[1] = $row['bid'];
        array_push($myArray, $tempArray);
    }
}
echo json_encode ($myArray, JSON_NUMERIC_CHECK);
  • 写回答

1条回答 默认 最新

  • dougang5088 2014-06-07 08:57
    关注

    At the end of the code, use $myArray instead of $tempArray

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面