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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误