PHPL determine whether a data table exists in the MYSQ database

Source: Internet
Author: User
This article is mainly to share with you PHPL Judge mysq database data table exists, there are two main ways, hope to help everyone.

Note: The following are all tested in the thinkphp framework

Mode 1

By querying the configuration table information for MySQL

Table_schema: Represents the database name, TABLE_NAME: Represents the 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];

Mode 2

Queries all data tables in the specified data, and then compares the queried data tables for existence

Detects if the 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.