Lou Posted March 9, 2012 Share Posted March 9, 2012 Hello again, I'm converting to mysqli OOP for connecting to my MySQL database. Previously I was using procedural mysqli. Basically, if I a connection can't be made to MYSQLI, I have a handle_error function that takes a user error message and a system error message and then uses the header redirect to my show_error page. I start the session in the config file, which is required on every page. It works when using procedural mysqli. However I can't get it to redirect using MYSQLI objects. It just shows me a white page with the system error message when it can't connect to MYSQL, like it's throwing an exception. I thought only PDO threw exceptions? I must be missing something simple, but I haven't been able to figure it out. Any help would be appreciated. <?php require_once('./config.php'); $connection = new mysqli(HOST, USER, PASS, DB); if ($connection == NULL){ handle_error('Message One', 'Message Two'); } else { return $connection; } ?> And the handle_error function in my config file: function handle_error($user_error_message, $system_error_message){ //handle all errors with this function $_SESSION['user_error_message'] = $user_error_message; $_SESSION['system_error_message'] = $system_error_message; header("Location: ./show_error.php"); } Link to comment Share on other sites More sharing options...
Lou Posted March 9, 2012 Author Share Posted March 9, 2012 This always happens. I spend a few hours trying to solve a problem, post on here, then figure it out. Why does the solution below work with OOP but not what I posted above? <?php require_once('./config.php'); $connection = new mysqli(HOST, USER, PASS, DB); if (mysqli_connect_error()){ handle_error('Message One', 'Message Two'); break; } else { return $connection; } ?> Link to comment Share on other sites More sharing options...
rob Posted March 9, 2012 Share Posted March 9, 2012 In your script you're testing a mysqli object ($connection) to NULL; from the manual: OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples. http://www.php.net/manual/en/mysqli.construct.php Link to comment Share on other sites More sharing options...
Lou Posted March 9, 2012 Author Share Posted March 9, 2012 thanks rob, still getting used to oop. Link to comment Share on other sites More sharing options...
Recommended Posts