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
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.
The operating environment of this article: Windows 10 system, ThinkPHP5 version, Dell G3 computer.
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!