本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選