php檢測mysql表是否存在的方法小結
更新時間:2017年07月20日 09:27:58 作者:3wlog
這篇文章主要介紹了php檢測mysql表是否存在的方法,結合實例形式總結分析了php使用pdo連接及mysql函數(shù)實現(xiàn)針對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("數(shù)據(jù)庫連接失敗".$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基于pdo操作數(shù)據(jù)庫技巧總結》、《php+Oracle數(shù)據(jù)庫程序設計技巧總結》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP 正則表達式之正則處理函數(shù)小結(preg_match,preg_match_all,preg_replace,pr
本節(jié)我們就來介紹一下PHP中基于perl的正則表達式處理函數(shù),主要包含了分割, 匹配,查找,替換等等處理操作,依舊是配合示例講解,讓我們開始吧2012-10-10

