Check if column exists in mysql table with php

Hi Experts!

As in title, I need a php script checking if column exists in table.

Thanks for any help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The "information_schema" table in MySQL is a special, internal table that holds all the information about the database structure. You can query it like this:

SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table'

You can use that method / query to check for column existence.
ZadoAuthor Commented:
Thanks gr8gonzo, I wrote this:
$checkcolumn = mysql_query("SELECT $column FROM information_schema.columns WHERE table_name = '$tablename'");
if (mysql_num_rows($checkcolumn)>0) {echo "column does not exists!";} else {echo "column exists!";}

Open in new window

but it doesn't work as expected, I keep trying...
ZadoAuthor Commented:
Ok, got it, it's probably not the best to do it, but it works, if you know better way, please let me know:

$tablename = $_GET['table'];
		$column = "UserID";
		$columnlist = array();
		$checkcolumn = mysql_query("SELECT column_name FROM information_schema.columns WHERE table_name = '$tablename'");
		while ($row = mysql_fetch_array($checkcolumn)) {
			$columnlist[] = $row['column_name'];
		$columnlist = implode(',',$columnlist);
		if (strpos($columnlist,$column)!==false) {echo "column exists!";} else {echo "column does not exists!";}

Open in new window

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

ZadoAuthor Commented:
*up  'it's probably not the best way to do it'
Sorry, try this:

$checkcolumn = mysql_query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='{$tablename}' AND COLUMN_NAME = '{$column}';");
if (mysql_num_rows($checkcolumn)>0) {echo "column does not exists!";} else {echo "column exists!";}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZadoAuthor Commented:
Thanks :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.