Edward
Edward

Reputation: 1883

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.

$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!

Upvotes: 0

Views: 1196

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157889

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

Upvotes: 2

Related Questions