phpl determines whether the data table in the mysq database exists

小云云
Release: 2023-03-22 11:38:01
Original
2094 people have browsed it

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];
Copy after login

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));
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template