php判断表是否存在的方法

yipeiwu_com5年前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+iframe模拟Ajax上传文件功能示例

PHP+iframe模拟Ajax上传文件功能示例

本文实例讲述了PHP+iframe模拟Ajax上传文件功能。分享给大家供大家参考,具体如下: 在xmlhttprequest level 1中,Ajax是不能够上传文件的,因为js不能操...

微信 开发生成带参数的二维码的实例

微信开发生成带参数的二维码的讲解 在微信公众号平台开发者那里,在“账号管理”那里,有一项功能是“生成带参数的二维码”,通过这儿生成的二维码,只要通过微信扫一扫之后,会把事件自动推送到微...

php error_log 函数的使用

我们来大致了解一下error_log()函数。我们看下手册的解释: error_log(PHP 3, PHP 4, PHP 5) bool error_log ( string mess...

php字符串比较函数用法小结(strcmp,strcasecmp,strnatcmp及strnatcasecmp)

本文实例分析了php字符串比较函数用法。分享给大家供大家参考,具体如下: 直接比较字符串是否完全一致,可以使用"=="来进行,但是有时候可能需要进行更加复杂的字符串比较,如部分匹配等....

洪恩在线成语词典小偷程序php版

主要函数是file_get_contents,主程序分两段,跟我一起看过来吧(凡人博客原创代码,转载请注明)。 复制代码 代码如下: function escape($str){ pre...