Jump to content

script fails if db doesn't connect


cloudll

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by grissom
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.