php判斷表是否存在的方法
更新時間:2015年06月18日 09:22:23 作者:瘋狂的流浪
這篇文章主要介紹了php判斷表是否存在的方法,實例分析了三種常見的判斷表的方法,涉及php操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了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程序設(shè)計有所幫助。
相關(guān)文章
如何使用Laravel Eloquent來開發(fā)無限極分類
在網(wǎng)上商城上,我們經(jīng)??梢钥吹蕉嗉壏诸?、子分類、甚至無限極分類。本文將向你展示如何優(yōu)雅的通過 Laravel Eloquent 將其實現(xiàn)。2021-05-05php將csv文件導(dǎo)入到mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了php將csv文件導(dǎo)入到mysql數(shù)據(jù)庫的方法,通過讀取csv文件到數(shù)組再調(diào)用while循環(huán)實現(xiàn)插入數(shù)據(jù)到數(shù)據(jù)庫,是非常實用的技巧,需要的朋友可以參考下2014-12-12給PHP開發(fā)者的編程指南 第一部分降低復(fù)雜程度
給PHP開發(fā)者的編程指南,第一部分的內(nèi)容是降低復(fù)雜程度,感興趣的小伙伴們可以參考一下2016-01-01PHP實現(xiàn)的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實現(xiàn)的mysql操作類,結(jié)合實例形式分析了MySQL與MySQLi方式連接與操作MySQL數(shù)據(jù)庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10