《PHP教程:php判斷表是否存在的方法》要點:
本文介紹了PHP教程: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程序設計有所幫助.PHP實戰
維易PHP培訓學院每天發布《PHP教程:php判斷表是否存在的方法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。