Home >Backend Development >PHP Tutorial >PHP implements method to detect whether mysql table exists
This article mainly introduces the method of PHP detecting the existence of mysql table. It summarizes and analyzes the method of PHP using pdo connection and mysql function to determine the existence of mysql table in the form of examples. Friends who need it can refer to it
pdo:
<?php $dsn = 'mysql:dbname=test;host=127.0.0.1'; $user = 'root'; $password = ''; try { $pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { die("数据库连接失败".$e->getMessage()); } $table = 'cy_news'; //判断表是否存在 $result = $pdo->query("SHOW TABLES LIKE '". $table."'"); $row = $result->fetchAll(); if('1' == count($row)){ echo "Table exists"; } else { echo "Table does not exist"; } ?>
##mysql:
<?php $con = mysql_connect("localhost","root",""); mysql_select_db("php_cms", $con); $table = 'cy_news'; if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '". $table."'"))==1) { echo "Table exists"; } else { echo "Table does not exist"; } ?>Related recommendations:
Detailed explanation of the database being unable to start due to damage to the MySQL table data file
How to use mysql tableConnection
mysql tableSummary of whether it exists
The above is the detailed content of PHP implements method to detect whether mysql table exists. For more information, please follow other related articles on the PHP Chinese website!