本篇文章主要介绍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 (&<a>本2文来*源gao($daima.com搞@代@#码(网</a><strong>搞gaodaima代码</strong>#39;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中使用curl伪造IP的功能
php使用MagickWand模块操作图片添加水印的方法
php针对当前编码的判断与对应的编码转换实现技巧
以上就是php操作数据库判断表是否存在的方法的详细内容,更多请关注搞代码gaodaima其它相关文章!