Php checks whether a table exists. php checks whether the table exists.

Source: Internet
Author: User

Php checks whether a table exists. php checks whether the table exists.

This example describes how to use php to determine whether a table exists. Share it with you for your reference. The details are as follows:

<? Php // method 1 mysql_connect ('localhost', 'root', '000000') or die ('can \'t not connect database'); if (int) check_table_is_exist ('show databases; ', 'test') = 1) {echo' the table exists ';} else {echo' The table does not exist ';} function check_table_is_exist ($ SQL, $ find_table) {$ row = mysql_query ($ SQL); $ database = array (); $ finddatabase = $ find_table; while ($ result = mysql_fetch_array ($ row, MYSQL_ASSOC) {$ database [] = $ result ['database'];} u Nset ($ result, $ row); mysql_close ();/* start to determine whether the table exists */if (in_array ($ find_table, $ database) {return true ;} else {return false ;}} //////////////////////////////////////// ///// method 2 mysql_connect ('localhost ', 'root', 'root'); $ result = mysql_list_tables ('database'); $ I = 0; while ($ I <mysql_num_rows ($ result )) {if ('table _ name' = mysql_tablename ($ result, $ I) {echo 'exists'; break;} $ I ++;} echo 'doesn't exist'; mysql_clo Se (); /// // method 3 $ data = array (); $ dbname = 'name of the table you want to query '; mysql_connect ('localhost', 'root', '') or die ('cann \' t connect server! '); $ Result = mysql_query ('show databases;'); While ($ row = mysql_fetch_assoc ($ result )) {$ data [] = $ row ['database'];} unset ($ result, $ row); mysql_close (); print_r ($ data ); if (in_array (strtolower ($ dbname), $ data) die ('exist'); else die ('nonexistent ');?>

I hope this article will help you with php programming.

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.