5

The below code works perfectly and gives me the "id" "firstname" and "lastname". What I want is to use a loop to echo all the field values in the result row without having to quote each column name like

$row["id"]

below is the working code

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

 // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . 
$row["lastname"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?> 

any thoughts please

3 Answers 3

1

This should work..

while($row = $result->fetch_array()) {
  $i=0;
  while($i<count($row)){
    echo $row[$i];
    $i++;
  }  
}
Sign up to request clarification or add additional context in comments.

9 Comments

I got an error for this as below: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at webmaster@*****.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Its not related to php code.. answer updated.. check again.. notice fetch_array()
I edited the code to be like below; while($row = $result->fetch_array()) { $i=0; while($i<count($row)){ echo $row[$i]; $i++; } } but it still gives me only the first value "someuser".
I think the count($row) equals just one. I tried sizeof($row); and still the same result. Anymore thoughts?
try print_r($row) after $i=0.. and check if it having array of multiple key values..
|
1

just use a foreach loop inside while like this

foreach($row as $key=>$value){
 echo "<br> $key: ". $value. "<br>";
}

2 Comments

This answer gives me only the first column: " Username: Someuser"
as @Zaheer Attar suggested, I figured out the mistake in my query. Your code works just fine as is and outputs "Username: Someuser" and so on for all columns. Thanks.
0

If I understand what you want to do is automate the output of the name of each column with its value (independent of the number of columns you get).

So do it like this :

if ($result->num_rows > 0) {
    foreach($result->fetch_assoc() as $row) { // browse each records
        foreach($row as $col => $value) { // browse each columns on a record
            echo "<br>$col: $value<br>";
        }
    }
} 
else {
    echo "no result";
}

3 Comments

Tried it, it produces <br> <br><br><br><br> continuously for the number of columns in the record.
it prints Array()Array()Array()Array()Array() continuously for the number of columns.
Thanks, but I got the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.