通过php检查Mysql表中是否存在列

时间:2022-09-25 23:21:09

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

我创建了mysql表,在那里我放了一些列。

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

现在我想通过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 :)

非常感谢你的时间:)

5 个解决方案

#1


26  

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

有关更多信息: - MySQL,检查SQL中是否存在列

Note:- mysql_* is deprecated use mysqli or PDO

注意: - 不推荐使用mysq_ *使用mysqli或PDO

#2


0  

You can use mysql_list_fields and mysql_num_fields to get columns of a table

您可以使用mysql_list_fields和mysql_num_fields来获取表的列

$fields = mysql_list_fields('database_name', 'table_name');
$columns = mysql_num_fields($fields);

#3


0  

In PHP:

在PHP中:

$fields = mysql_list_fields('database_name', 'table_name');
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}

if (!in_array('price', $field_array))
{
$result = mysql_query('ALTER TABLE table_name ADD price VARCHAR(10)');
}

This should also help you:

这也应该对你有所帮助:

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ‘TEST’ AND COLUMN_NAME = ‘TEST_DATE’)
BEGIN
  ALTER TABLE TEST ADD TEST_DATE DATETIME
END

Or you can do:

或者你可以这样做:

Show columns from table like 'string';

Check out this question : How can I check if mysql table column even exists?

看看这个问题:如何检查mysql表列是否存在?

#4


0  

If your database is MySQL then, you can use mysql_field_name() and mysql_fetch_field() function in php.

$res = mysql_query('select * from customer', $link);

echo mysql_field_name($res, 0); // print cust_id
echo mysql_field_name($res, 1); // print cust_name 
echo mysql_field_name($res, 2); // print cust_age

#5


0  

Run this query in php and check if number of rows > 0 :- DESC tablename '%columns_name%'

在php中运行此查询并检查行数> 0: - DESC tablename'%columns_name%'

#1


26  

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

有关更多信息: - MySQL,检查SQL中是否存在列

Note:- mysql_* is deprecated use mysqli or PDO

注意: - 不推荐使用mysq_ *使用mysqli或PDO

#2


0  

You can use mysql_list_fields and mysql_num_fields to get columns of a table

您可以使用mysql_list_fields和mysql_num_fields来获取表的列

$fields = mysql_list_fields('database_name', 'table_name');
$columns = mysql_num_fields($fields);

#3


0  

In PHP:

在PHP中:

$fields = mysql_list_fields('database_name', 'table_name');
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}

if (!in_array('price', $field_array))
{
$result = mysql_query('ALTER TABLE table_name ADD price VARCHAR(10)');
}

This should also help you:

这也应该对你有所帮助:

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ‘TEST’ AND COLUMN_NAME = ‘TEST_DATE’)
BEGIN
  ALTER TABLE TEST ADD TEST_DATE DATETIME
END

Or you can do:

或者你可以这样做:

Show columns from table like 'string';

Check out this question : How can I check if mysql table column even exists?

看看这个问题:如何检查mysql表列是否存在?

#4


0  

If your database is MySQL then, you can use mysql_field_name() and mysql_fetch_field() function in php.

$res = mysql_query('select * from customer', $link);

echo mysql_field_name($res, 0); // print cust_id
echo mysql_field_name($res, 1); // print cust_name 
echo mysql_field_name($res, 2); // print cust_age

#5


0  

Run this query in php and check if number of rows > 0 :- DESC tablename '%columns_name%'

在php中运行此查询并检查行数> 0: - DESC tablename'%columns_name%'