Bill Karwin wrote:[color=blue]
> <phillip.s.powell@gmail.com> wrote in message
> news:1142542142.682445.31650@i40g2000cwc.googlegro ups.com...[color=green]
> > I am trying to write a SQL statement that will tell me if a table
> > exists or not, this is because PHP will throw Fatal Errors and halt all
> > programming if the table does not exist that I attempt to query.[/color]
>
> That's true -- if your PHP code invokes the die() function on error.
> You should do something different when you detect an error.
>
> $link = mysql_connect(. . .);
> if (!mysql_query($sql, $link)) {
> if (mysql_errno($link) == 1146) { // 1146 is ER_NO_SUCH_TABLE
> // do something to handle the problem
> }
> }
>
> See errno 1146 and others at
>
http://dev.mysql.com/doc/refman/5.0/...es-server.html
>[color=green]
> > How do you write an elegant MySQL 4.1+ statement to detect if a table
> > exists or not?[/color]
>
> SHOW TABLES LIKE 'name' should be all you need. If the query result
> produces no rows, then the table doesn't exist.
>
> But this really shouldn't be the solution. The best solution is to write
> code that queries tables that do exist. Failing that, handle the error with
> something other than die().
>
> Regards,
> Bill K.[/color]
Thanx, I figured it out, using a PHP solution to be able to detect if
it exists or not, using a new method in my DBConnection object called
list_tables() that returns an array of tables using the
mysql_list_tables($db, $dbconnection) function.
if ($subselectTableName) {
// USE INHERITED $dbAP->dbConnObj METHOD list_tables($dbDefaultName)
if (@in_array($subselectTableName,
$dbAP->dbConnObj->list_tables($dbDefaultName))) {
$query = new MySQLQuery("SELECT count(*) AS numRows FROM
$subselectTableName LIMIT 1", $dbAP->getDBConn());
$rowResult =@ $query->getResult();
}
}
This will insure that either the table exists and runs the query, or it
doesn't and does not run the query, and no errors as a result!
Thanx!
Phil