php检测mysql表是否存在 php检测mysql表是否存在的方法小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

php检测mysql表是否存在 php检测mysql表是否存在的方法小结

3wlog   2021-03-24 我要评论
想了解php检测mysql表是否存在的方法小结的相关内容吗,3wlog在本文为您仔细讲解php检测mysql表是否存在的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php,检测,mysql表,是否存在,下面大家一起来学习吧。

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

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

希望本文所述对大家PHP程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们