MySQL 테이블 중복 체크
<?php
function table_check1($table,$handle)
{
$result = mysql_query("CHECK TABLE {$table}",$handle);
$row = mysql_fetch_assoc($result);
if ( $row['Msg_text']=='OK' ) return true;
return false;
}
function table_check2($table,$handle)
{
$result = mysql_query("SELECT 1 FROM {$table}",$handle);
if ( is_resource($result) ) return true;
return false;
}
function table_check3($table,$handle)
{
$result = mysql_query("SHOW TABLES LIKE '{$table}'",$handle);
$row = mysql_fetch_assoc($result);
if ( $row ) return true;
return false;
}
function table_check4($table,$handle)
{
$result = mysql_query("DESC {$table}",$handle);
if ( is_resource($result) ) return true;
return false;
}
function table_check5($db,$table,$handle)
{
$result = mysql_list_tables($db);
$table_list = array();
while ( $row=mysql_fetch_assoc($result) ) $table_list[] = $row['Tables_in_mysql'];
if ( in_array($table,$table_list) ) return true;
return false;
}
// 사용 예.
$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'db';
$dbconn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$dbconn);
echo table_check1('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check2('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check3('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check4('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check5($db,'table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
mysql_close($dbconn);
?>
function table_check1($table,$handle)
{
$result = mysql_query("CHECK TABLE {$table}",$handle);
$row = mysql_fetch_assoc($result);
if ( $row['Msg_text']=='OK' ) return true;
return false;
}
function table_check2($table,$handle)
{
$result = mysql_query("SELECT 1 FROM {$table}",$handle);
if ( is_resource($result) ) return true;
return false;
}
function table_check3($table,$handle)
{
$result = mysql_query("SHOW TABLES LIKE '{$table}'",$handle);
$row = mysql_fetch_assoc($result);
if ( $row ) return true;
return false;
}
function table_check4($table,$handle)
{
$result = mysql_query("DESC {$table}",$handle);
if ( is_resource($result) ) return true;
return false;
}
function table_check5($db,$table,$handle)
{
$result = mysql_list_tables($db);
$table_list = array();
while ( $row=mysql_fetch_assoc($result) ) $table_list[] = $row['Tables_in_mysql'];
if ( in_array($table,$table_list) ) return true;
return false;
}
// 사용 예.
$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'db';
$dbconn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$dbconn);
echo table_check1('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check2('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check3('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check4('table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
echo table_check5($db,'table_name',$dbconn) ? '있다' : '없다';
echo '<br>';
mysql_close($dbconn);
?>
참고로 $handle 생략 가능합니다.
함께 나눈 이야기
댓글댓글 남기기