Posted in PHP onJuly 20, 2017
本文实例讲述了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程序设计有所帮助。
php检测mysql表是否存在的方法小结
- Author -
3wlog声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@