Vivid Lust Posted August 26, 2008 Share Posted August 26, 2008 If there is a sql error redirect to a page... Anyway to do this??? such that: <?php if(SQL ERROR){ header('location:http://foo.com'); } Thanks if you know if there is anyway to do this. Link to comment https://forums.phpfreaks.com/topic/121422-if-there-is-a-sql-error-redirect-to-a-page/ Share on other sites More sharing options...
phoenixx Posted August 26, 2008 Share Posted August 26, 2008 // Here is the function. This function must be placed before any HTML code or // else the redirect will not work. You could place it in a separate file and // use include. function dbconnect($hostname, $username, $password, $database) { $conn = @mysql_pconnect($hostname, $username, $password); // "@" is necessary. if(!$conn) header("Location: error.html"); if(!mysql_select_db($database, $conn)) header("Location: error.html"); } Link to comment https://forums.phpfreaks.com/topic/121422-if-there-is-a-sql-error-redirect-to-a-page/#findComment-626119 Share on other sites More sharing options...
Vivid Lust Posted August 26, 2008 Author Share Posted August 26, 2008 I just have clarified a specific error, the error is when an illegal character is trying to be added as a table name and there is an error ... any ideas on how to redirect that? Link to comment https://forums.phpfreaks.com/topic/121422-if-there-is-a-sql-error-redirect-to-a-page/#findComment-626138 Share on other sites More sharing options...
revraz Posted August 26, 2008 Share Posted August 26, 2008 When you do your query, use a result and if there is an error send them to a error page. Link to comment https://forums.phpfreaks.com/topic/121422-if-there-is-a-sql-error-redirect-to-a-page/#findComment-626140 Share on other sites More sharing options...
obsidian Posted August 26, 2008 Share Posted August 26, 2008 The best way to handle specific errors is to use PHP5 and take advantage of Exception handling. This makes your life incredibly easier. Here is a very simplistic example of error handling to do what you are after: <?php /** * Define your DBException class to trap DB errors */ class DBException extends Exception {} try { if (mysql_connect(/* your connection info */) === FALSE) { throw new DBException('connection_error.php'); } } catch (DBException $e) { $file = $e->getMessage(); header("Location: $file"); exit; } catch (Exception $e) { die('Unexpected error encountered: ' . $e->getMessage()); } ?> Of course, a much better way to handle things would be to assign the error numbers to anything you wish and handle all the exceptions by error number rather than passing the filename through the message string, but this at least gives you the principle of exception handling. Link to comment https://forums.phpfreaks.com/topic/121422-if-there-is-a-sql-error-redirect-to-a-page/#findComment-626145 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.