Solved

Check if column exists in mysql table with php

Posted on 2012-04-10
6
1,890 Views
Last Modified: 2012-04-10
Hi Experts!

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

Thanks for any help.
0
Comment
Question by:Zado
  • 4
  • 2
6 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37828526
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.
0
 
LVL 8

Author Comment

by:Zado
ID: 37828623
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...
0
 
LVL 8

Author Comment

by:Zado
ID: 37828685
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

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Author Comment

by:Zado
ID: 37828692
*up  'it's probably not the best way to do it'
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37828697
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

0
 
LVL 8

Author Closing Comment

by:Zado
ID: 37828829
Thanks :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now