Re: Finding out if a table exists

From: Date: Tue, 21 Jan 2003 18:20:56 +0000
Subject: Re: Finding out if a table exists
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-132476@lists.php.net to get a copy of this message
Or... function mysql_table_exists($table) { mysql_query("use $table"); if(mysql_error()) { return false; } else { return true; } } ---John Holmes... ----- Original Message ----- From: "Kevin Stone" <kevin@helpelf.com> To: "Mako Shark" <phpman2000@yahoo.com>; <php-general@lists.php.net> Sent: Tuesday, January 21, 2003 1:05 PM Subject: Re: [PHP] Finding out if a table exists > Okay what the hell I'm bored anyway... > > function mysql_table_exists($table) > { > global $db; // <- database resource handle > $result = @mysql_list_tables($db); > if (mysql_num_rows($result) > 0) > { > while ($row = mysql_fetch_row($result)) > { > if (strtolower($row[0]) == strtolower($table))) > { > return true; > } > } > } > return false; > } > > Untested but it's pretty basic stuff so I'm certain it works. Enjoy! > > -Kevin > > ----- Original Message ----- > From: "Mako Shark" <phpman2000@yahoo.com> > To: <php-general@lists.php.net> > Sent: Tuesday, January 21, 2003 10:05 AM > Subject: [PHP] Finding out if a table exists > > > > Is there a way of finding if a table exists with only > > one command in MySQL? I've looked through the MySQL > > functions, and the closest I've gotten to what I need > > is mysql_list_tables or mysql_tablename (I'll have to > > check into these a little more), but I was hopefully > > looking for something that returns a boolean value, > > and to be able to use it like: > > > > if(mysql_table_exists("tablename")) { > > ... > > } > > > > Any ideas, short of creating my own function? > > > > __________________________________________________ > > Do you Yahoo!? > > Yahoo! Mail Plus - Powerful. Affordable. Sign up now. > > http://mailplus.yahoo.com > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#132476) next »