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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23513479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:46:31  来源:igfitidea点击:

Check if column exist in Mysql table via php

phpmysql

提问by user3611408

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 :)

非常感谢您的时间:)

回答by Rakesh Sharma

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 mysqlior PDO

注意:- mysql_* 已被弃用mysqliPDO

回答by kdrmlhcn

Suppose you want to find the customer who has placed at least one sales order, you can use the EXISTS operator as follows:

假设您要查找至少下过一个销售订单的客户,您可以使用 EXISTS 运算符,如下所示:

SELECT customerNumber,
         customerName
FROM customers
WHERE EXISTS
    (SELECT 1
    FROM orders
    WHERE orders.customernumber = customers.customernumber);

For each row in the customers table, the query checks the customerNumber in the orders table.

对于customers 表中的每一行,查询都会检查orders 表中的customerNumber。

If the customerNumber, which appears in the customers table, exists in the orders table, the subquery returns the first matching row. As the result, the EXISTS operator returns true and stops scanning the orders table. Otherwise, the subquery returns no row and the EXISTS operator returns false.

如果出现在customers 表中的customerNumber 存在于orders 表中,则子查询返回第一个匹配的行。结果,EXISTS 运算符返回 true 并停止扫描订单表。否则,子查询不返回任何行并且 EXISTS 运算符返回 false。

To get the customer who has not placed any sales orders, you use the NOT EXISTS operator as the following statement:

要获取尚未下任何销售订单的客户,请使用 NOT EXISTS 运算符作为以下语句:

SELECT customerNumber,
         customerName
FROM customers
WHERE NOT EXISTS
    (SELECT 1
    FROM orders
    WHERE orders.customernumber = customers.customernumber);

回答by Giovax68

This is simple but it works for me:

这很简单,但对我有用:

1) Select all from the table you want.

1)从你想要的表中选择所有。

$qry = "SELECT * FROM table";

2) Bring the result and verify if the field exists.

2) 带来结果并验证该字段是否存在。

if( $result = $mysqli->query($qry)  &&  isset($result['field_you_want']) ){
    //Exists
}else{
    //Doesn't exists
}

回答by lampdev

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);

回答by Bla...

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 表列是否存在?

回答by dev4092

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

回答by Vandana

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

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

回答by velkoon

Ok I spent 5 hours on this issue looking through every (sometimes ridiculously convoluted) StackOverflow answer, never finding a working answer, and the answer is so frustratingly simple you will kick yourself (as did I):

好吧,我在这个问题上花了 5 个小时浏览了每个(有时令人费解的)StackOverflow 答案,但从未找到有效的答案,答案非常简单,您会踢自己(就像我一样):

php 7

php 7

$mysqli = new mysqli($mysql_url, $mysql_user, $mysql_password, $db_name);

$column_name = "my_column";
$table_name = "my_table";

if ($mysqli->query("SELECT $column_name FROM $table_name")){
        //my_column exists in my_table
    }
else{
        //my_column doesn't exist in my_table
}

Databases 101....

数据库 101....