MySQL – PHP PDO Get All Column Names from Table in Specific Database

MySQLPHP

I currently have the following query. Which fetches all column names for a table. But when there are 2 tables with the same name (in different databases) it fetches those column names as well.

In the following query $table of course means the requested table. In this case for example customer.

select column_name from information_schema.columns 
where lower(table_name)=lower(\''.$table.'\')

So my question is: How do I specify which database the query should look into.

Best Answer

In MySQL database is a synonym for schema. Subsequently, the information you're looking for is in the column table_schema:

select column_name from information_schema.columns 
where lower(table_name)=lower(\''.$table.'\')
  and lower(table_schema)=lower(\''.$mydb.'\')