Home >Backend Development >PHP Tutorial >PHP method to determine whether a table exists, PHP determines whether a table exists_PHP tutorial

PHP method to determine whether a table exists, PHP determines whether a table exists_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:49:40883browse

How to determine whether a table exists in php, how to determine whether a table exists in php

The example in this article describes how to determine whether a table exists in php. Share it with everyone for your reference. The details are as follows:

<&#63;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('不存在');
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1019080.htmlTechArticleHow to determine whether a table exists in php, how to determine whether a table exists in php. This example describes how to determine whether a table exists in php. Share it with everyone for your reference. The details are as follows: php//Method 1 mysq...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn