php判斷表是否存在的方法
更新時間:2015年06月18日 09:22:23 作者:瘋狂的流浪
這篇文章主要介紹了php判斷表是否存在的方法,實例分析了三種常見的判斷表的方法,涉及php操作數據庫的相關技巧,需要的朋友可以參考下
本文實例講述了php判斷表是否存在的方法。分享給大家供大家參考。具體如下:
<?php //方法一 mysql_connect('localhost','root','2260375') or die('can\'t not connect database'); if((int)check_table_is_exist('show databases;','test')==1) { echo '該表存在'; } else { echo '該表不存在'; } function check_table_is_exist($sql,$find_table) { $row=mysql_query($sql); $database=array(); $finddatabase=$find_table; while ($result=mysql_fetch_array($row,MYSQL_ASSOC)) { $database[]=$result['Database']; } unset($result,$row); mysql_close(); /*開始判斷表是否存在*/ if(in_array($find_table,$database)) { return true; } else { return false; } } //////////////////////////////////////////////方法二 mysql_connect('localhost','root','root'); $result = mysql_list_tables('database'); $i=0; while($i<mysql_num_rows($result)) { if ('Table_Name' == mysql_tablename($result,$i)) { echo '存在'; break; } $i++; } echo '不存在'; mysql_close(); //////////////////////////////////////方法三 $data = array(); $dbname = '你要查詢的表名'; mysql_connect('localhost', 'root', '') or die('Cann\'t connect server!'); $result = mysql_query('show databases;'); While($row = mysql_fetch_assoc($result)){ $data[] = $row['Database']; }unset($result, $row); mysql_close(); print_r($data); if (in_array(strtolower($dbname), $data)) die('存在'); else die('不存在'); ?>
希望本文所述對大家的php程序設計有所幫助。
相關文章
如何使用Laravel Eloquent來開發(fā)無限極分類
在網上商城上,我們經??梢钥吹蕉嗉壏诸悺⒆臃诸?、甚至無限極分類。本文將向你展示如何優(yōu)雅的通過 Laravel Eloquent 將其實現。2021-05-05PHP實現的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實現的mysql操作類,結合實例形式分析了MySQL與MySQLi方式連接與操作MySQL數據庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10