cloudll Posted May 22, 2015 Share Posted May 22, 2015 Hey guys, I swear I used to have this working. If my database connection fails my page doesn't load at all. Theres not even html output. Here is my database connect code. $hostname = 'localhost'; $database = 'engine'; $username = 'root'; $password = ''; try { $connect = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); } catch(PDOException $e) { error_reporting(0); } // CONNECTION STATUS if (!$connect) { $connStatus = "Database connection failed"; } else { $connStatus = "Connected to database"; } Does anyone know why It would completly stop the page from loading? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/296454-script-fails-if-db-doesnt-connect/ Share on other sites More sharing options...
rwhite35 Posted May 22, 2015 Share Posted May 22, 2015 You'll want to set your PDOException parameter. $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Then it should report any errors correctly. What is your $connStatus reporting? Connected to database? Quote Link to comment https://forums.phpfreaks.com/topic/296454-script-fails-if-db-doesnt-connect/#findComment-1512477 Share on other sites More sharing options...
cloudll Posted May 22, 2015 Author Share Posted May 22, 2015 (edited) Yep its just simply if its connected or not. The error it gives me is this line: $sql = "SELECT * FROM encounter WHERE id='1'"; foreach ($connect->query($sql) as $row) { Edited May 22, 2015 by cloudll Quote Link to comment https://forums.phpfreaks.com/topic/296454-script-fails-if-db-doesnt-connect/#findComment-1512487 Share on other sites More sharing options...
grissom Posted May 22, 2015 Share Posted May 22, 2015 (edited) Hmm, I don't want to put words in rwhite's mouth, but I think he was asking ... if you add the line : echo $connStatus; immediately after your block of code above, what does it say that $connStatus is equal to ? I don't know enough about what is in the rest of your code or not, but from the bit I've seen, I'm wondering if your query should go something like $sql = "SELECT * FROM encounter WHERE id='1'"; $result = $connect->query($sql); while ($rowOfData = $result->fetch_row() { // whatever .. } Edited May 22, 2015 by grissom Quote Link to comment https://forums.phpfreaks.com/topic/296454-script-fails-if-db-doesnt-connect/#findComment-1512488 Share on other sites More sharing options...
mac_gyver Posted May 23, 2015 Share Posted May 23, 2015 when the database connection fails and you run a statement that uses the connection variable, like - foreach ($connect->query($sql) as $row) you get a fatal runtime error that halts code execution at that statement. when something fails, you need to take an appropriate action, which would include not running any code that's dependent on whatever failed. why are you setting error_reporting to zero in the catch{} block of code? for development, you would want to report and display all errors, not hide them and on a live server you would want to report all errors, but log them rather than display them. Quote Link to comment https://forums.phpfreaks.com/topic/296454-script-fails-if-db-doesnt-connect/#findComment-1512508 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.