Home  >  Article  >  Backend Development  >  phpl determines whether the data table in the mysq database exists

phpl determines whether the data table in the mysq database exists

小云云
小云云Original
2018-03-27 10:19:572086browse

This article mainly shares with you how to use phpl to determine whether the data table in the mysq database exists. There are two main ways. I hope it can help everyone.

Note: The following are all tested in the ThinkPHP framework

Method 1

By querying MySQL configuration table information

//TABLE_SCHEMA:表示数据库名 , TABLE_NAME : 表示表名  
$sql = "SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name' and TABLE_NAME = 'table_name'";   
$model = new \Think\Model();  
$res = $model->query($sql);
$isExist = $res[0];

Method 2

Query all data tables in the specified data, and then compare whether the queried data table exists

//检测表是否存在
function tableExist($tableName){
    if(empty($tableName)) return false;
    $tableName = C('DB_PREFIX').$tableName;
    $model = new \Think\Model();
    $tableArr = $model->query('SHOW TABLES');
    $_fName = 'tables_in_'.C('DB_NAME');
    return in_array($tableName, array_column($tableArr, $_fName));
}

Related recommendations:

Mysql data table operation examples detailed explanation

php and mysql create a data table and obtain the content and render it to the front-end page

View the index method of the MySQL data table

The above is the detailed content of phpl determines whether the data table in the mysq database exists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn