Nicolas
Nicolas

Reputation: 1143

MYSQL foreach Row Echo Data

How do I echo out every column's data of a row from MYSQL results?

I do not know what the rows are as the query is dynamically created.

Here's what I have:

$query = $_POST['query'];
// Create connection
$con=mysqli_connect($db_host, $db_user, $db_pass, $db_name);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$results = mysqli_query($con, $query);

while ($row = mysqli_fetch_array($results)) {

}

Upvotes: 1

Views: 2460

Answers (1)

John Conde
John Conde

Reputation: 219824

$row is just an array. Like any other array you can do fun things like iterate over it:

while ($row = mysqli_fetch_array($results)) {
    foreach ($row as $key => $value) {
        echo 'Key: ' . $key . ', Value: ' . $value;
    }
    echo  "<br><br>\n"
}

Upvotes: 3

Related Questions