반응형
MySQL 테이블의 존재 여부 확인
중복 가능성:
MySQL 예외를 발생시키지 않고 테이블이 존재하는지 확인
프로젝트에 다른 테이블에서 선택 쿼리를 만드는 동적 mysql 쿼리 작성기가 있습니다.
현재 진행 중인 테이블이 있는지 확인해야 합니다.
제 테이블이 1번 테이블, 2번 테이블, 3번 테이블이라고 상상해보세요.내 코드는 다음과 같습니다.
<?php
for($i = 1 ; $i <= 3 ; $i++) {
$this_table = 'table'.$i;
$query = mysql_query("SELECT * FROM $this_table");
// ...
}
?>
이 확인은 어떻게 해야 하나요(가장 간단한 방법을 알려주세요).
mysqli 버전 업데이트:
if ($result = $mysqli->query("SHOW TABLES LIKE '".$table."'")) {
if($result->num_rows == 1) {
echo "Table exists";
}
}
else {
echo "Table does not exist";
}
원본 mysql 버전:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1)
echo "Table exists";
else echo "Table does not exist";
PHP 문서에서 참조.
다른 게시물에서 가져옴
$checktable = mysql_query("SHOW TABLES LIKE '$this_table'");
$table_exists = mysql_num_rows($checktable) > 0;
$query = mysqli_query('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME IN ("table1","table2","table3") AND TABLE_SCHEMA="yourschema"');
$tablesExists = array();
while( null!==($row=mysqli_fetch_row($query)) ){
$tablesExists[] = $row[0];
}
$result = mysql_query("SHOW TABLES FROM $dbname");
while($row = mysql_fetch_row($result))
{
$arr[] = $row[0];
}
if(in_array($table,$arr))
{
echo 'Table exists';
}
이 쿼리를 사용한 다음 결과를 확인합니다.
$query = 'show tables like "test1"';
해보세요.
$query = mysql_query("SELECT * FROM $this_table") or die (mysql_error());
또는 이
$query = mysql_query("SELECT * FROM $this_table") or die ("Table does not exists!");
또는 이
$query = mysql_query("SELECT * FROM $this_table");
if(!$query)
echo "The ".$this_table." does not exists";
도움이 되길 바랍니다!
MySQL 방법:
SHOW TABLES LIKE 'pattern';
모든 db 테이블을 나열하는 데 사용되지 않는 PHP 기능도 있습니다. http://php.net/manual/en/function.mysql-list-tables.php 을 참조하십시오.
그 링크를 확인해 보세요, 저기 댓글에 유용한 통찰력이 많이 있습니다.
언급URL : https://stackoverflow.com/questions/9008299/check-if-mysql-table-exists-or-not
반응형
'bestsource' 카테고리의 다른 글
'root'@'localhost' 사용자(암호 사용: YES)에 대한 액세스가 거부되었습니다(Mysql::오류) (0) | 2023.08.07 |
---|---|
파이썬에서 모듈과 라이브러리의 차이점은 무엇입니까? (0) | 2023.08.07 |
SQL Server에서 varbinary를 문자열로 변환하는 중 (0) | 2023.08.07 |
마크다운 및 이미지 정렬 (0) | 2023.08.07 |
중지 설정 간격 (0) | 2023.08.02 |