PHP - MySQL select, from, where

PHP - MySQL select, from, where

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

Stefan Bogdanescu

Founder & Senior Architect

PHP - MySQL select, from, where

The error message Unknown column 'password' in 'field list' is a direct indicator of a schema mismatch between your SQL query and your database table definition. You are attempting to execute a SELECT statement targeting a column named password, but the database engine cannot find that specific identifier within the users table you are querying.

When you run a query like $query = "SELECT password FROM users...", the MySQL server parses the string before execution. If the metadata for the users table does not contain a column strictly named password, the server halts immediately and throws that fatal error. This is distinct from a runtime logic issue; the code fails during the compilation phase because the requested resource simply does not exist in the specified location.

The Legacy Stack Problem

Looking closely at your implementation, there is a more critical architectural issue than the missing column: you are using the mysql_* functions (mysql_query, mysql_result). This extension was deprecated in PHP 5.5 and removed entirely in PHP 7.0. While it might still run on older servers or local environments with legacy PHP versions, relying on it introduces severe security vulnerabilities and compatibility barriers.

The specific line causing immediate failure in modern contexts is not just the missing column, but the injection vulnerability inherent in your query construction:

$query = "SELECT password FROM users WHERE username = $loggin_user";

By interpolating $_COOKIE['username'] directly into the SQL string without sanitization, you are opening your application to SQL injection attacks. An attacker could manipulate the cookie value to bypass authentication or extract other data from the database. Even if you fixed the column name error tomorrow, this query structure is fundamentally unsafe.

Schema Verification

Before writing complex application logic, you must verify the actual structure of the users table. The error implies that the column definition does not match your expectation. Run the following command in your MySQL client to inspect the table schema:

DESCRIBE users;

or

SHOW COLUMNS FROM users;

This will return a list of all columns, their data types, and null constraints. You might find that the column is named passwd, pass, or perhaps it is stored in a different table entirely. It is also possible that the column exists but is defined with a reserved keyword name like password without backticks, though standard SQL usually handles this fine unless the specific MySQL version has quirks regarding reserved words.

If the column truly does not exist, you will need to alter the table or correct your query logic if you are mistakenly targeting the wrong table alias. Once the schema matches your query intent, the Unknown column error will resolve itself.

Modernizing the Query

Moving away from the deprecated mysql_* functions is essential for any production environment. The standard replacement in native PHP is mysqli, which supports prepared statements to handle user input safely. Here is how you would refactor the code assuming the column name was correct and using mysqli:

$host = 'localhost';
$db   = 'your_database';
$user = 'your_username';
$pass = 'your_password';

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Use prepared statements to prevent SQL injection
$stmt = $conn->prepare("SELECT password FROM users WHERE username = ?");
$username = $_COOKIE['username'];
$stmt->bind_param("s", $username); // 's' indicates a string type
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $password = $row['password'];
} else {
    // Handle case where user was not found
    $password = null; 
}

$stmt->close();
$conn->close();

Security and Best Practices

Even after correcting the column name, echoing a raw password retrieved from a database is almost never the correct behavior. Passwords should be stored as hashed values (e.g., using bcrypt or argon2) in the database, not plaintext. When retrieving them, you should compare the stored hash against the hash of the user's current input rather than checking for equality of strings.

If you are building a new project or migrating an existing one, consider adopting Laravel. It provides robust tools for handling database connections and authentication that abstract away the low-level SQL syntax errors you are currently debugging. For more on modern PHP development practices and security, resources like https://laravelcompany.com offer guidance on securing applications and managing state effectively.

Finally, always verify your database schema against your application code. A simple DESCRIBE command can save hours of debugging time compared to guessing column names or assuming a legacy structure persists into modern implementations.

Our network