1. Problem Description
When using MySQL, you may frequently encounter errors like these:
Unknown column 'media' in 'field list'
Unknown column 'media_type' in 'field list'
This error occurs when a query references a column that does not exist. It is especially common when using PHP and MySQL together.
2. Root Causes
The "Unknown column" error typically stems from the following causes:
(1) The column doesn't actually exist
The column is referenced in the query but has never been created in the table.
(2) Typo in the column name
For example: medai, meida_type — simple typos.
(3) Table structure was changed but the code wasn't updated
The column was deleted from the database, but the code still references it.
(4) Querying the wrong table
Incorrect table specified in a JOIN or FROM clause.
(5) Backtick (`) or quote issues
The column name is mistakenly interpreted as a string literal.
3. Solutions
Follow these steps in order — most cases will be resolved this way.
✔ 1. Check the Table Structure
sqlDESCRIBE table_name;
Or:
sqlSHOW COLUMNS FROM table_name;
👉 Verify whether the column actually exists.
✔ 2. Correct the Column Name
sql-- Incorrect
SELECT media FROM posts;
-- Correct
SELECT image FROM posts;
✔ 3. Add the Column (if needed)
sqlALTER TABLE posts ADD media VARCHAR(255);
✔ 4. Sync Your PHP Code with the Database
The column names in your PHP code must match those in the database exactly.
php$sql = "INSERT INTO posts (title, content, media) VALUES (?, ?, ?)";
👉 The media column must exist in the database.
✔ 5. Verify the Table Name
sqlSELECT * FROM post; -- ❌ Wrong table name
SELECT * FROM posts; -- ✅ Correct table name
4. Code Examples
A side-by-side comparison of error-causing code and the fixed version:
❌ Code That Causes the Error
php$sql = "INSERT INTO posts (title, content, media_type) VALUES (?, ?, ?)";
👉 Will throw an error if the media_type column doesn't exist.
✅ Fix 1: Add the Missing Column
sqlALTER TABLE posts ADD media_type VARCHAR(50);
✅ Fix 2: Update the Code to Match the Schema
php$sql = "INSERT INTO posts (title, content) VALUES (?, ?)";
5. Summary
The "Unknown column" error is simple but frequently encountered. Checking these four things will resolve almost every case:
Verify the column exists
Check for typos in the column name
Ensure the database and code are in sync
Use the correct table name
💡 Pro Tip
During development, table structures change often. Always keep your database schema and application code in sync to prevent this type of error.
Login with Google