dreamphp기록과 추억이 머무는 곳
Tip&Tech

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);

?>

참고로 $handle 생략 가능합니다.
목록

함께 나눈 이야기

댓글

댓글 남기기

이미지와 영상은 본문에 표시됩니다. 삭제한 첨부와 본문 연결은 수정 저장할 때 함께 반영됩니다.

취소

작성자 삭제 후에도 삭제 안내가 남으며 운영자는 원문을 확인할 수 있습니다.