retrieve data from db and display it in table in php .. see this code whats wrong with it?

retrieve data from db and display it in table in php .. see this code whats wrong with it?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging Legacy PHP: Why Your mysql_* Table Output Fails

If you are looking at this code and wondering why your database table isn't displaying, the issue lies in a combination of deprecated function usage and a critical syntax error inside your PHP block. The snippet provided attempts to connect to a MySQL server using the legacy mysql_* extension, perform an insert, and then render a table. While the logic for fetching rows seems sound on paper, the actual execution will crash before it even reaches the HTML generation phase.

The most glaring error is located within the while loop. The code snippet shows:

<? 
while ($array = mysql_fetch_row($result));
{
    print "<tr> <td>";
    echo $array[0]; 
    // ...
}
?>

There is a stray semicolon immediately following the while condition: while ($array = mysql_fetch_row($result));. In PHP, that semicolon terminates the loop definition. Consequently, the code block inside the braces { ... } executes exactly once as a standalone script, rather than iterating over every row returned by the query. After the first iteration, $array holds the last row fetched, and the loop simply ends. If your database has more than one record, the rest of the data remains hidden. Furthermore, if the initial insert or select fails due to an error (which is highly likely given the lack of error handling), the script will halt entirely without ever entering the loop at all.

Beyond the syntax error, relying on the mysql_* functions is a significant architectural risk. These extensions have been deprecated and removed in PHP 7.0 and later versions. They offer no protection against SQL injection and lack support for prepared statements. The code constructs the query string directly using variables like $name and $mail:

$query = "insert into names values('$name','$add1','$add2','$mail')";

This approach is vulnerable to script injection attacks. An attacker could supply a value like ' OR '1'='1 in the $name variable, causing the database to execute unintended commands or expose all data in the table. Modern PHP development requires moving away from this pattern entirely. The mysqli extension or PDO (PHP Data Objects) are the current standards. They provide procedural and object-oriented interfaces that support prepared statements, ensuring that user input is treated strictly as data rather than executable code.

Assuming you have migrated to a modern stack like PDO, the logic for retrieving and displaying data becomes much safer and cleaner. You would bind parameters to your query, preventing injection vulnerabilities automatically. When fetching results, using PDOStatement::fetch() or fetchAll() into an array is preferred over the older mysql_fetch_row syntax, though the underlying concept of iterating through a result set remains similar.

If you are maintaining legacy code that must still run on older PHP versions, fixing the semicolon placement is the immediate step to get the table rendering. However, for any new project or refactoring effort, the path forward involves replacing mysql_connect and mysql_query with PDO. This migration isn't just about fixing a broken loop; it's about securing your application against common web vulnerabilities.

Resources like https://laravelcompany.com often highlight the importance of modernizing stacks to ensure long-term maintainability. They discuss how frameworks abstract these lower-level database interactions, making it easier to enforce security best practices without writing raw SQL strings manually. If you are building a new table-viewing feature today, consider using a templating engine or a framework helper to generate the HTML structure, rather than concatenating strings inside a loop, which reduces the chance of further syntax errors and improves code readability.

Our network