duanhuang2804 2013-05-15 14:40
浏览 6
已采纳

从所有行获取所有字段 - MYSQL

I understand this could appear alarming but bear with me.

I need to echo every field in every row in a table.

This is only an example - I have removed the HTML wrapped around it to improve readability.

$a = 1;

while ($a <= $count_rows) {
    $query = "SELECT col1, col2 etc.. FROM table WHERE `id`='$id'";

    $result = mysqli_query($con, $query);
    $i = 1; 

    while($i <= $count_fields) {
        $row = mysqli_fetch_array($result, MYSQL_NUM);
        echo "$row[$i]";
        $i++;       
    }

    $a++;
    $id = $a;
}

This only outputs the first field of every row? Why?

If I echo $row[2] I get nothing!

  • 写回答

1条回答 默认 最新

  • duanlu1922 2013-05-15 14:44
    关注

    If I echo $row[2] I get nothing!

    because it's actually third item
    and there is some strange code interfering with $i variable

    Anyway, to get every column from every row ou need a code like this

    $query = "SELECT * FROM table";
    $result = mysqli_query($con, $query);
    while($row = mysqli_fetch_row($result)) {
        foreach ($row as $index => $value) {
            echo "$index => $value, ";
        }
        echo "<br>
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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