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在字符串中查找指定字符串并删除的代码

$a = "abcababa"; $count=strpos($a,"ab"); $str=substr_replace($a,"",$count,2); 输出结果:cababa 代码虽...

PHP使用自定义方法实现数组合并示例

PHP使用自定义方法实现数组合并示例

本文实例讲述了PHP使用自定义方法实现数组合并。分享给大家供大家参考,具体如下: 在PHP中提供了强大的数组功能,对于数组的合并也提供了两个方法:array_merge 和 array_...

MongoDB在PHP中的常用操作小结

$mongodb = new Mongo(); //$connection = new Mongo( "$dburl:$port" ); // connect to a remote h...

PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程

PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下: 一、PHP检测页面 check.php...

PHP array_shift()用法实例分析

本文实例讲述了PHP array_shift()用法。分享给大家供大家参考,具体如下: array_shift()将数组开头的单元移出数组,并作为结果返回,将数组长度减一并将所有其它单元...