Rumah  >  Artikel  >  pembangunan bahagian belakang  >  利用php查询mysql表是否存在的方法总结

利用php查询mysql表是否存在的方法总结

黄舟
黄舟asal
2018-05-11 17:42:366333semak imbas

这篇文章主要介绍了php检测mysql表是否存在的方法,结合实例形式总结分析了php使用pdo连接及mysql函数实现针对mysql表存在的判断方法,需要的朋友可以参考下

本文实例讲述了php检测mysql表是否存在的方法。分享给大家供大家参考,具体如下:

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";
}
?>

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";
}
?>

Atas ialah kandungan terperinci 利用php查询mysql表是否存在的方法总结. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn