query('SHOW TABLES LIKE" table name"'); if ($a){exists}else{table does not exist}" to judge; 2. Use "$a =M(table name);if($a->getDbError()){table does not exist}else{exists}" judgment."/> query('SHOW TABLES LIKE" table name"'); if ($a){exists}else{table does not exist}" to judge; 2. Use "$a =M(table name);if($a->getDbError()){table does not exist}else{exists}" judgment.">

Home >PHP Framework >ThinkPHP >How to determine if a table exists in thinkphp5

How to determine if a table exists in thinkphp5

WBOY
WBOYOriginal
2022-02-15 11:30:153926browse

Method: 1. Use "$a=M()->query('SHOW TABLES LIKE" table name"'); if ($a){exists}else{table does not exist}" to determine ;2. Use "$a=M(table name);if($a->getDbError()){table does not exist}else{exists}" to judge.

How to determine if a table exists in thinkphp5

The operating environment of this article: Windows 10 system, ThinkPHP5 version, Dell G3 computer.

How to judge whether a table exists in thinkphp5

The thinkphp framework is a very easy-to-use and very powerful framework. It is the development habit of our people. So when using the thinkphp framework, sometimes there is a need to determine whether a certain database table exists.

Here, we assume to determine whether the users table exists:

$users = M('users');
if($users->getDbError()){
    echo '数据表不存在!';
}else{
    echo '数据库存在!';
}

Method 2:

$isTable = M()->query('SHOW TABLES LIKE "table_name"');
if( $isTable ){
    echo '表存在';
}else{
    echo '表不存在';
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine if a table exists in thinkphp5. 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