Output (echo/print) everything from a PHP Array
The immediate challenge in your scenario is that mysql_fetch_array returns an associative array containing columns like 'id', 'name', or 'status'. When you iterate through these rows, accessing $row['id'] explicitly retrieves only that specific column. To dump the entire contents of the array for debugging or inspection without hardcoding column names, you must iterate over the array itself rather than accessing it by key.
In PHP, the standard approach to achieve this is using a foreach loop. This construct iterates directly over the elements of an array, making it ideal for printing every value present in the row object. Here is how you would refactor your existing code:
while ($row = mysql_fetch_array($result)) {
foreach ($row as $key => $value) {
echo "$key: $value\n";
}
}
This snippet prints every key-value pair found in the row, including hidden fields like mysql_type or mysql_num_rows if they are part of the returned structure. If you simply need to output the raw serialized string without formatting, you can use print_r($row) or var_dump($row). However, these functions print a single line that requires manual scrolling to read, whereas the foreach loop above allows for cleaner, column-by-column inspection.
It is critical to address the underlying security and stability risks in your source code before implementing any output logic. You are currently using the mysql_* extension (mysql_fetch_array). This extension has been deprecated since PHP 5.5.0 and removed entirely in PHP 7.0.0. Attempting to run this code on modern PHP versions will result in a fatal error. Furthermore, the mysql_* functions do not support prepared statements, leaving your application vulnerable to SQL injection attacks if the database queries are constructed dynamically.
If you are maintaining legacy code or migrating a project, you should switch to either MySQLi or PDO. These libraries provide robust methods for handling rows and executing secure queries. For instance, using MySQLi with prepared statements looks like this:
$stmt = $mysqli->prepare("SELECT * FROM users");
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
echo "$key: " . htmlspecialchars($value) . "\n";
}
}
Notice the use of fetch_assoc(). While mysql_fetch_array returns both associative and numeric keys by default, modern best practices favor explicitly choosing the return format. Using fetch_assoc() ensures you get an associative array where keys match column names, which makes debugging via foreach much more predictable than dealing with mixed integer indices.
There is also a specific trade-off regarding how you handle special characters in the output. When echoing raw data from a database, especially text fields, you risk breaking your HTML layout or introducing XSS vulnerabilities if the user input isn't escaped. Always ensure that whatever method you use to output the array contents respects the context of your page (HTML vs. plain text). If you are building an admin panel where you need to inspect data without rendering it as HTML, setting the output buffer to text mode or using htmlspecialchars is essential.
Finally, consider whether you actually need to print every field. Often, debugging logs require specific columns for troubleshooting, not a dump of every internal index. If you are using PDO, the row object returned by fetch() behaves slightly differently depending on the fetch mode set via $stmt->setFetchMode(). Ensuring consistency in how rows are fetched will prevent unexpected numeric indices from appearing in your output loop, making the debugging process cleaner and more reliable.