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实现在对象之外访问其私有属性private及保护属性protected的方法

本文实例讲述了PHP实现在对象之外访问其私有属性private及保护属性protected的方法。分享给大家供大家参考,具体如下: public 表示全局的访问权限,类内部外部子类都可以...

PHP strtotime函数用法、实现原理和源码分析

源码位置:\ext\date\php_date.c 复制代码 代码如下: /* {{{ proto int strtotime(string time [, int now ]) &nb...

基于php使用memcache存储session的详解

web服务器的php session都给memcached ,这样你不管分发器把 ip连接分给哪个web服务器都不会有问题了,配置方法很简单,就在php的配置文件内增加一条语句就可以了,...

ThinkPHP跳转页success及error模板实例教程

本文以实例讲解了ThinkPHP跳转页面的success与error方法所对应的视图与控制器的实现方法,通过本实例教程可以帮助读者更好的掌握success方法与error方法的使用。 首...

PHP Socket 编程

下面是相应的代码: PHP 代码: 复制代码 代码如下: <? // 设置一些基本的变量 $host = "192.168.1.99"; $port = 1234; // 设置超时...