码迷,mamicode.com
首页 > 数据库 > 详细

mysql判断是否存在表

时间:2019-11-20 19:43:11      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:mysql   sts   function   new   数据   tables   except   use   tab   

1、在Yii2框架中判断数据表是否存在。

private static function checkTable($table){
    $sql = "SHOW TABLES LIKE ‘". $table."‘";
    $existTable = static::findBySql($sql)->asArray()->one();
    if(empty($existTable)){
        return 0;
    }
    return 1;
}

2、在php中判断数据表是否存在。

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

mysql判断是否存在表

标签:mysql   sts   function   new   数据   tables   except   use   tab   

原文地址:https://blog.51cto.com/11315052/2451772

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!