[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Check if column exists in mysql table with php

Posted on 2012-04-10
6
Medium Priority
?
2,620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 35

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 8

Author Comment

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

Accepted Solution

by:
gr8gonzo earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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