Jump to content

How to check the mysql table exists or not.


girish.kc

Recommended Posts

Hi,

I am creating a temporary table. I need to check if the table already exists or not. If exists, I have to use the existing data else have to create a new temporary table.

 

I can use 'Create If Not Exists'. How practical is this query? any issues with the performance? If the table exists it will be a huge data, I don't want to loose that data because recreating is an un-necessary load on the database.

 

 

Hi,

<?
function table_exists ($table, $db) { 
$tables = mysql_list_tables ($db); 
while (list ($temp) = mysql_fetch_array ($tables)) {
	if ($temp == $table) {
		return TRUE;
	}
}
return FALSE;
}

/** How to use it **/
if (table_exists(test_table, my_database)) {
echo"Yes the table is there.";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.