本文實例講述了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相關內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《php+Oracle數(shù)據(jù)庫程序設計技巧總結》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學習交流,請勿用于商業(yè)用途。如損害你的權益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。