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

