简体   繁体   English

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

[英]Fetch all fields from all rows - 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. 这只是一个例子 - 我已经删除了包裹它的HTML以提高可读性。

$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! 如果我回应$row[2]我什么都没得到!

If I echo $row[2] I get nothing! 如果我回应$ row [2]我什么都没得到!

because it's actually third item 因为它实际上是第三
and there is some strange code interfering with $i variable 并且有一些奇怪的代码干扰了$ i变量

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>\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM