Why You Should Stop Using the MySQL Extension in PHP
Looking at the code snippet provided, the most immediate red flag is the use of the deprecated mysql_* extension. This library has been removed from the core PHP distribution for years. It lacks support for prepared statements, meaning every query in that script is inherently vulnerable to SQL injection attacks. Furthermore, it does not support multiple database connections or modern features like transactions and stored procedures effectively. The fact that you are asking what is wrong with it suggests a need to migrate away from this legacy API entirely, likely toward mysqli or PDO.
Immediate Security Vulnerabilities
The primary technical flaw in the provided query is the concatenation of strings directly into the SQL statement:
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
If the value 'Admin' were to come from user input, an attacker could inject malicious SQL code. Even though this specific example uses a hardcoded string, relying on manual concatenation is a bad practice. The mysql_* functions do not enforce prepared statements, so you must manually escape every single variable. This leads to bloated, error-prone code.
In contrast, modern extensions like mysqli or PDO allow you to prepare the statement first and then bind parameters. This separates the SQL logic from the data, ensuring that user input is always treated as data rather than executable code. For example, using mysqli, you would write:
$stmt = $conn->prepare("SELECT * FROM users WHERE name = ?");
$stmt->bind_param("s", $name); // 's' indicates a string type
$stmt->execute();
$result = $stmt->get_result();
Logical Errors in the Provided Script
Beyond security, there are logical errors in the execution flow. The line $rows = $rows['Name']; inside the while loop is incorrect. It overwrites the entire result array with a single scalar value from the 'Name' column. In the subsequent lines, you attempt to access $rows['Address']. Since $rows no longer holds an array but a string (or potentially null), accessing array keys on it will throw a fatal error or return unexpected results.
The correct approach is to iterate over the result set using fetch_assoc() or keep the row as an array:
while ($row = $result->fetch_assoc()) {
echo $row['Name'] . '<br>';
echo $row['Address'] . '<br>';
}
Migration to Modern Standards
The mysql_* extension was deprecated in PHP 5.5 and removed in PHP 7.0. If your server is running a version of PHP released after that date, this code will simply not run. You must migrate to either the Object-Oriented interface of MySQLi or PDO.
PDO is generally preferred for its database abstraction capabilities, allowing you to switch database drivers (like PostgreSQL or SQLite) with minimal code changes. However, if you are already heavily invested in MySQL features, mysqli offers a direct upgrade path without changing your connection syntax significantly.
The Role of Frameworks
If you are building a new project or maintaining an existing one where security and maintainability are priorities, writing raw SQL queries is rarely the best path forward. Modern PHP frameworks like Laravel handle database interactions through an elegant query builder and an efficient ORM (Eloquent). This abstracts away the connection management, prepared statements, and result fetching entirely.
For instance, in Laravel, you would not write mysql_connect or mysql_select_db. You would define a migration to create the table and then access data via a Model:
use App\Models\User;
$user = User::where('name', 'Admin')->first();
echo $user->name . '<br>';
echo $user->address . '<br>';
This approach enforces type safety, handles relationships naturally, and ensures that your application remains compatible with future PHP versions. If you need further guidance on implementing these patterns or migrating legacy codebases to modern stacks, resources like https://laravelcompany.com can provide specific insights into adopting these architectural changes.
Conclusion
The code in question is obsolete primarily because it relies on a removed API that cannot enforce security best practices. The immediate fix involves switching to mysqli or PDO and implementing prepared statements, but the long-term solution is to adopt modern database abstraction layers. Continuing to use legacy code risks both security breaches and compatibility issues as the PHP ecosystem evolves.