Php Check If Column Exists Mysql
I am trying to write a query that will check if a specific table in MySQL has a specific column, and if not — create it. Otherwise do nothing. This is really an easy procedure in any enterprise-class database, yet MySQL seems to be an exception.
MySQL, Check if a column exists in a table with SQL. Ask Question 108. I am trying to write a query that will check if a specific table in MySQL has a specific column, and if not — create it. Otherwise do nothing. This is really an easy procedure in any enterprise-class database, yet MySQL seems to be an exception. Check if column.
I thought something like
would work, but it fails badly. Is there a way?
Gareth DavisIn this article, we show how to check if a MySQL table exists using PHP. Using the code below, you can check to see if a MySQL table exists. So once we make the connection to the database, we can then check to see if the table exists. Code to Check if a MySQL Table Exists. Check if a MySQL table exists Posted in MySql - Last updated Jun. MySQL has a couple of ways (that I know of, there may be more) of working out if a table exists. This post looks at how to check if a table exists in the MySQL database.
10 Answers
This works well for me.
With PHP it would be something like..
MfooMfoo@julio
Thanks for the SQL example. I tried the query and I think it needs a small alteration to get it working properly.
That worked for me.
Thanks!
forsvarirJust to help anyone who is looking for a concrete example of what @Mchl was describing, try something like
SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_table' AND COLUMN_NAME = 'my_column'
If it returns false (zero results) then you know the column doesn't exist.
radbyxFollowing is another way of doing it using plain PHP without the information_schema database:
Select just column_name from information schema and put the result of this query into variable. Then test the variable to decide if table needs alteration or not.
P.S. Don't foget to specify TABLE_SCHEMA for COLUMNS table as well.
MchlMchlI threw this stored procedure together with a start from @lain's comments above, kind of nice if you need to call it more than a few times (and not needing php):
Working with fieldExists
I am using this simple script:
It works if you are already connected to the database.
DO NOT put ALTER TABLE/MODIFY COLS or any other such table mod operations inside a TRANSACTION. Transactions are for being able to roll back a QUERY failure not for ALTERations..it will error out every time in a transaction.
Just run a SELECT * query on the table and check if the column is there..
Many thanks to Mfoo who has put the really nice script for adding columns dynamically if not exists in the table. I have improved his answer with PHP. The script additionally helps you find how many tables actually needed 'Add column' mysql comand. Just taste the recipe. Works like charm.
webbloverwebbloverNot the answer you're looking for? Browse other questions tagged mysql or ask your own question.
Mysql If Exists Select
I have a procedure that should check if a record exists or not for particular date range, if exists then fetch the record else fetch last 20 record.
For this i have to write a query multiple times, one for checking the existance , then fetch the same record or fetch record without where clause but with limit .
Php Check If Column Exists Mysql Free
Query goes something like this inside procedure
Is there any way i can do it in a single query because my query has too many joins and too complicated, it may take some amount of time, so if i call it two times the fetching time will increase.
hot2use2 Answers
You could probably reduce this to 2 queries with something like:
markpmarkpThis probably eliminates the extra 'result set' (and is faster than using COUNT(*)
):