15

i want to echo out everything from a particular query. If echo $res I only get one of the strings. If I change the 2nd mysql_result argument I can get the 2nd, 2rd etc but what I want is all of them, echoed out one after the other. How can I turn a mysql result into something I can use?

I tried:

$query="SELECT * FROM MY_TABLE";
$results = mysql_query($query);
$res = mysql_result($results, 0);

while ($res->fetchInto($row)) {
    echo "<form id=\"$row[0]\" name=\"$row[0]\" method=post action=\"\"><td style=\"border-bottom:1px solid black\">$row[0]</td><td style=\"border-bottom:1px solid black\"><input type=hidden name=\"remove\" value=\"$row[0]\"><input type=submit value=Remove></td><tr></form>\n";
}
MPelletier
  • 16,256
  • 15
  • 86
  • 137
pg.
  • 2,503
  • 4
  • 42
  • 67

6 Answers6

27
$sql = "SELECT * FROM MY_TABLE";
$result = mysqli_query($conn, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) { // Important line, returns assoc array
    echo "<tr>";
    foreach ($row as $field => $value) { 
        echo "<td>" . htmlspecialchars($value) . "</td>"; 
    }
    echo "</tr>";
}
echo "</table>";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
17

NOTE: In 2018, long after I wrote this, the accepted answer was significantly improved.

Expanding on the accepted answer:

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);
echo "<table>" ;
$first_row = true;
while ($row = mysqli_fetch_assoc($result)) {
    if ($first_row) {
        $first_row = false;
        // Before displaying first row of data.
        // ---> Display row of column headers, from keys. <---
        echo '<tr>';
        foreach($row as $column_name => $value) {
            echo '<th>' . htmlspecialchars($column_name) . '</th>';
        }
        echo '</tr>';
    }
    echo '<tr>';
    // Display row of data.
    foreach($row as $column_name => $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
    }
    echo '</tr>';
}
echo "</table>" ;

Benefits compared to accepted answer's 2018 version:

  • Shows field names as a header row (row of column headers) of table.

Example Output:

Id      Name
777     Aardvark
50      Lion
9999    Zebra
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
7
 $result= mysql_query("SELECT * FROM MY_TABLE");
 while($row = mysql_fetch_array($result)){
      echo $row['whatEverColumnName'];
 }
Babiker
  • 18,300
  • 28
  • 78
  • 125
1
$sql = "SELECT * FROM YOUR_TABLE_NAME";
$result = mysqli_query($conn, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) { // Important line !!!
    echo "<tr>";
    foreach ($row as $field => $value) { // If you want you can right this line like this: foreach($row as $value) {
        echo "<td>" . $value . "</td>"; 
    }
    echo "</tr>";
}
echo "</table>";

In PHP 7.x You should use mysqli functions and most important one in while loop condition use "mysqli_fetch_assoc()" function not "mysqli_fetch_array()" one. If you would use "mysqli_fetch_array()", you will see your results are duplicated. Just try these two and see the difference.

DeathPro
  • 98
  • 6
1

Nested loop to display all rows & columns of resulting table:

$rows = mysql_num_rows($result);
$cols = mysql_num_fields($result);
for( $i = 0; $i<$rows; $i++ ) {
   for( $j = 0; $j<$cols; $j++ ) {
     echo mysql_result($result, $i, $j)."<br>";
   }
}

Can be made more complex with data decryption/decoding, error checking & html formatting before display.

Tested in MS Edge & G Chrome, PHP 5.6

Zimba
  • 2,854
  • 18
  • 26
0

All of the snippets on this page can be dramatically reduced in size.

The mysqli result set object can be immediately fed to a foreach() (because it is "iterable") which eliminates the need to maked iterated _fetch() calls.

Imploding each row will allow your code to correctly print all columnar data in the result set without modifying the code.

$sql = "SELECT * FROM MY_TABLE";
echo '<table>';
    foreach (mysqli_query($conn, $sql) as $row) {
        echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
    }
echo '</table>';

If you want to encode html entities, you can map each row:

implode('</td><td>' . array_map('htmlspecialchars', $row))

If you don't want to use implode, you can simply access all row data using associative array syntax. ($row['id'])

mickmackusa
  • 43,625
  • 12
  • 83
  • 136