2012年1月11日水曜日

phpとmysqlでテーブルの存在確認

<?php
    $host = "localhost";
    $user   = "root";
    $pass = "mysql";
    $db = "test";
 $con = mysql_connect($host,$user,$pass) or die("db接続NG");
 print "db接続OK";
//テーブルチェック関数の呼出
    if(table_exists($db,'item',$con)){
     print "item table hit";
    }else{
     print "item table none";
    }
 mysql_close();


// テーブルの存在チェック関数の定義  start
 function table_exists($db_name,$tbl_name,$db)
 {
  // テーブルリストの取得
  $rs = mysql_query("SHOW TABLES FROM $db_name");
  while($arr_row = mysql_fetch_row($rs)){
    if(in_array($tbl_name,$arr_row)){
      return true;
    }
  }
  return false;
 }
// テーブルの存在チェック関数の定義  end
?>

0 件のコメント:

コメントを投稿