Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if column exist in MySQL table via PHP

Tags:

php

mysql

I created mysql tables and there I have put some columns.

Now I want to check that a certain column exists in the database via php.

Like this:

if (column exist){
echo "Column in table is available."
}
else{
echo "Column doesnt exist.";
}

Is it possible to do this?

A lot of thanks for your time :)

like image 528
user3611408 Avatar asked May 07 '14 09:05

user3611408


1 Answers

try

$result = mysql_query("SHOW COLUMNS FROM `table` LIKE 'fieldname'");
$exists = (mysql_num_rows($result))?TRUE:FALSE;
if($exists) {
   // do your stuff
}

For more :- MySQL, Check if a column exists in a table with SQL

Note:- mysql_* is deprecated use mysqli or PDO

like image 64
Rakesh Sharma Avatar answered Sep 18 '22 16:09

Rakesh Sharma