这篇文章给大家介绍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中怎么判断表是否存在就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。