php判断表是否存在的方法

yipeiwu_com6年前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正则表达式 /i, /is, /s, /isU等介绍

PHP正则表达式 /i, /is, /s, /isU等 都是些什么东西呢? i 不区分大小写 s 模式中的圆点元字符(.)匹配所有的字符,包括换行符 x 模式中的空白字符除了被转义的或在...

PHP的微信支付接口使用方法讲解

在开发之中经常会使用到支付的功能,现在常用的两种支付方式是支付宝和微信。相对而言,支付宝的文档较为健全,并且配置和调用方式方式比较简单,这里就不过多的描述。 首先去微信官网网站下去下载服...

php通过pecl方式安装扩展的实例讲解

1、安装pecl及创建快捷键(若安装php时已带可忽略这步安装步骤) # cd /usr/local/php/bin/ //可查看是否已带有pecl # wget http://...

php学习笔记之mb_strstr的基本使用

前言 本文主要介绍了关于php之mb_strstr基本使用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 mb_strstr (PHP 5 >...

php中base_convert()进制数字转换函数实例

本文实例讲述了php中base_convert()函数进制数字转换的实现方法。分享给大家供大家参考。具体如下: 语法:base_convert(number,frombase,tobas...