Summary of methods to use php to query whether a mysql table exists

黄舟
Release: 2023-03-14 11:02:02
Original
6361 people have browsed it

This article mainly introduces the method of PHP detecting the existence of mysql table. It summarizes and analyzes in the form of examples how PHP uses pdo connection and mysql function to realize the method of judging the existence of mysql table. Friends in need can refer to it

The example of this article describes the method of PHP detecting whether the mysql table exists. Share it with everyone for your reference, the details are as follows:

pdo:

<?php
$dsn = &#39;mysql:dbname=test;host=127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;&#39;;
try {
  $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
  die("数据库连接失败".$e->getMessage());
}
$table = &#39;cy_news&#39;;
//判断表是否存在
$result = $pdo->query("SHOW TABLES LIKE &#39;". $table."&#39;");
$row = $result->fetchAll();
if(&#39;1&#39; == count($row)){
  echo "Table exists";
} else {
  echo "Table does not exist";
}
?>
Copy after login

mysql:

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("php_cms", $con);
$table = &#39;cy_news&#39;;
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE &#39;". $table."&#39;"))==1) {
  echo "Table exists";
} else {
  echo "Table does not exist";
}
?>
Copy after login

The above is the detailed content of Summary of methods to use php to query whether a mysql table exists. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!