dongyanhu5628 2016-05-27 22:14
浏览 20
已采纳

PHP $ array [] = $内容不能正常工作

Okay, I have been tearing my hair (and beard) out at this one for a good hour or more now. I can't work out why it isn't working, particularly as it's exactly the same code I use elsewhere which does work.

$sql = "SELECT * FROM `aio_log` ORDER BY `log_timestamp` DESC LIMIT 10;"; // A 'WHERE' will be put in here
$logs = array();
$one_log = array();

if ($stmt = $mysqli->prepare($sql)) {
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($one_log['id'], $one_log['timestamp'],     $one_log['headline'],
                     $one_log['text'], $one_log['link'], $one_log['font_awesome'],
                     $one_log['type']);

  while ($stmt->fetch()) {
    if ($one_log['link'] == "") $one_log['link'] = '#';
    // print_r($one_log); shows the expected output of the database row.
    $logs[] = $one_log;
    //array_push($logs, $one_log); // gives the same - incorrect - response as the above line
  }
}
print_r($logs); // Shows the right number of returned rows from the database, but all showing
// an identical content of the last database row returned.

Probably the only difference between this code and code I've used elsewhere is that this one doesn't have any binded parts of the SQL statement as it's a simple 'select everything no matter what' statement, but as the print_r($one_log) bit works fine, I cannot see this being the issue. The issue is obviously somewhere in the $logs[] = $one_log bit, but as I've used it elsewhere, and get exactly the same result using array_push, I'm at my wits end here!

Ideas folks?

  • 写回答

1条回答 默认 最新

  • dreamwind1985 2016-05-27 22:34
    关注

    This is very strange. The assignment

    $logs[] = $one_log;
    

    is supposed to make a copy of $one_log when it pushes it onto the array. But apparently this isn't interacting as expected with the fact that bind_param binds references to the variables -- when you fetch the new row, the references are apparently referring to the copy as well as the original. This is probably a weird consequence of the way that PHP uses copy-on-write; updating the reference seems not to be considered a write that triggers the copy.

    I suggest you use normal variables instead of the $one_log array.

    $stmt->bind_result($id, $timestamp, $headline, $text, $link, $font_awesome, $type);
    

    and then in the fetch loop use:

    $logs[] = array('id' => $id, 'timestamp' => $timestamp, 'headline' => $headline, 
                    'text' => $text, 'link' => $link, 'font_awesome' => $font_awesome, 
                    'type' => $type);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL