dongsi073898 2015-10-07 19:44
浏览 46
已采纳

无法在数据库中输出所有数组行

Problem: Can only output one field from each array

 <?php
    error_reporting(0);
    session_start();
    include('connect.php');
    ob_start();

    $category = array(
        "dates" => array(),
        "paragraphs" => array(),
        "titles" => array()
    );

    if ($result = $mysqli->query("SELECT * FROM tips")){
        if($result->num_rows){
            while($row = $result->fetch_array(MYSQLI_ASSOC)){
            //printf ("%s %s %s
",$row["date"], $row["title"], $row["paragraph"]); 
            $dates= array($row["date"]);
            $titles = array($row["title"]);
            $paragraphs = array($row["paragraph"]);
            }
            }
            $result->free();
        }

    ?>

I want the last in the array to be displayed first so I'm using this

<?php echo $titles[count($titles)-1]?>

I think the problem is with my loop, I think its reassigning the array each time it loops round

  • 写回答

2条回答 默认 最新

  • douwudie8060 2015-10-07 19:50
    关注

    Each iteration of while creates a new array in your vars, which first position stores current row data iteration.

    It overwrites the last one each time, try not $data = array($row['data']) but $data[] = $row['data'].

    This way you push the values of each iteration into a new index of the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测