首页 > 开发 > PHP > 正文

php判断表是否存在的方法

2024-05-04 23:36:23
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php判断表是否存在的方法,实例分析了三种常见的判断表的方法,涉及php操作数据库的相关技巧,需要的朋友可以参考下

本文实例讲述了php判断表是否存在的方法。分享给大家供大家参考。具体如下:

 

 
  1. <?php 
  2. //方法一 
  3. mysql_connect('localhost','root','2260375') or die('can/'t not connect database'); 
  4. if((int)check_table_is_exist('show databases;','test')==1) 
  5. { 
  6. echo '该表存在'; 
  7. } 
  8. else 
  9. { 
  10. echo '该表不存在'; 
  11. } 
  12. function check_table_is_exist($sql,$find_table) 
  13. { 
  14. $row=mysql_query($sql); 
  15. $database=array(); 
  16. $finddatabase=$find_table; 
  17. while ($result=mysql_fetch_array($row,MYSQL_ASSOC)) 
  18. { 
  19. $database[]=$result['Database']; 
  20. } 
  21. unset($result,$row); 
  22. mysql_close(); 
  23. /*开始判断表是否存在*/ 
  24. if(in_array($find_table,$database)) 
  25. { 
  26. return true; 
  27. } 
  28. else 
  29. { 
  30. return false; 
  31. } 
  32. } 
  33. //////////////////////////////////////////////方法二 
  34. mysql_connect('localhost','root','root');  
  35. $result = mysql_list_tables('database');  
  36. $i=0;  
  37. while($i<mysql_num_rows($result)) 
  38. { 
  39. if ('Table_Name' == mysql_tablename($result,$i)) { 
  40. echo '存在'; 
  41. break; 
  42. }  
  43. $i++;  
  44. } 
  45. echo '不存在'; 
  46. mysql_close(); 
  47. //////////////////////////////////////方法三 
  48. $data = array(); 
  49. $dbname = '你要查询的表名'; 
  50. mysql_connect('localhost', 'root', '') or die('Cann/'t connect server!'); 
  51. $result = mysql_query('show databases;'); 
  52. While($row = mysql_fetch_assoc($result)){ 
  53. $data[] = $row['Database']; 
  54. }unset($result, $row); 
  55. mysql_close(); 
  56. print_r($data); 
  57. if (in_array(strtolower($dbname), $data)) 
  58. die('存在'); 
  59. else 
  60. die('不存在'); 
  61. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表