9 Replies - 4491 Views - Last Post: 21 February 2011 - 03:26 PM Rate Topic: -----

#1 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 04:15 AM

Hi,

I am trying to prevent the script from posting the connection details and display a connection error.
Using mysqli but how i even try it always seems to print out the error, using easyPhp 5.3.5.0 apache.
Could it be local settings?

I had no problem using Try and Catch , and prevent it with PDO, but now i am using mysqli with this app.
Been trying some different ways without any luck. (note : mysqli)

    public function Connect()
    {
      // Open db connection  
      $this->mySQLI = new mysqli($this->hostAddress, $this->dbUser, $this->dbPass, $this->dbName);
      if (!$this->mySQLI)
      {
        die('Connection to database failed');
      }
      else
      {
        return true;
      }        
    }


This post has been edited by heady89: 19 February 2011 - 04:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Preventing & Catching mysqli connection error?

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 04:22 AM

What you'll want to do is check to see if $this->mySQLI->connect_error is set to a non-NULL value. MySQLi->connect_error

public function Connect()
{
  // Open db connection  
  $this->mySQLI = new mysqli($this->hostAddress, $this->dbUser, $this->dbPass, $this->dbName);
  // MySQLi->connect_error was broken prior to PHP 5.2.9 and 5.3.0.  That's why we've got the OO version and the procedural version being checked.
  if(version_compare(PHP_VERSION, '5.2.9', '<='))
  {
    if(mysqli_connect_error())
    {
      error_log("SQL Connection error: " . $this->mySQLI->connect_error, 3);
      die('Connection to database failed');
    }
  }
  else if ($this->mySQLI->connect_error)
  {
    error_log("SQL Connection error: " . $this->mySQLI->connect_error, 3);
    die('Connection to database failed');
  }
  else
  {
    return true;
  }        
}

This post has been edited by Valek: 19 February 2011 - 04:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 04:33 AM

may I throw into the discussion that PHP’s other database layer (PDO) does all that by itself? i.e. if the connection fails, it will throw an exception, which you only have to catch.
Was This Post Helpful? 1
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 04:37 AM

Yes, he noted that in his initial post, citing that (for whatever reason) he is now using MySQLi instead.
Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 04:38 AM

argh, I shouldn’t answer when cooking …
Was This Post Helpful? 1
  • +
  • -

#6 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 05:12 AM

Thanks for the replies guys.
When testing at least it just breaks into general browser error saying connection lost(firefox,ie8).
So it no longer seems to print out the error from easyPhp, however one would imagine it would display the die message,
but instead it basically sits until it does a timeout of the page.

This post has been edited by heady89: 19 February 2011 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 03:26 PM

Essentially it spits out my php error messages no matter what, i can make a custom error but
not get rid of the code spitting out details about my password and username etc. Is this
something that is know to be a part of the later easyPhp versions and that you have
to turn it off or something?
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Preventing & Catching mysqli connection error?

Posted 19 February 2011 - 05:01 PM

MySQLI should not just spit out that message without you doing so yourself. It stores it in MySQLi->connect_error. Technically the one with credentials isn't a PHP error anyway, it's an error specific to the MySQLi extension (proof of this can be seen by using a MySQL driver of any kind in pretty much any other language that has one. You'll get the exact same error message for invalid credentials). PHP errors themselves are usually directly output to the browser, which you can have it avoid by setting the display_errors directive to off and the log_errors directive to on. Then it won't output the errors to your browser, it will instead automatically write them to a file you specify in the error_log directive. Default file is error_log.
Was This Post Helpful? 0
  • +
  • -

#9 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Preventing & Catching mysqli connection error?

Posted 21 February 2011 - 01:21 PM

View PostValek, on 19 February 2011 - 05:01 PM, said:

MySQLI should not just spit out that message without you doing so yourself. It stores it in MySQLi->connect_error. Technically the one with credentials isn't a PHP error anyway, it's an error specific to the MySQLi extension (proof of this can be seen by using a MySQL driver of any kind in pretty much any other language that has one. You'll get the exact same error message for invalid credentials). PHP errors themselves are usually directly output to the browser, which you can have it avoid by setting the display_errors directive to off and the log_errors directive to on. Then it won't output the errors to your browser, it will instead automatically write them to a file you specify in the error_log directive. Default file is error_log.


Ok so in my php.ini i set display_errors = Off and now it doesn't show anything, i guess that's what you're talking about aight?
If i have it on it will spit out the callback table errors(or whatever you call them), so i suspect the errors/info i was getting
is only related to the dev settings that errors should be displayed no matter what?

But like i said regarding the errors it now dosnt display anything error related. die("Connection not established") will for instance never show.

This post has been edited by heady89: 21 February 2011 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Preventing & Catching mysqli connection error?

Posted 21 February 2011 - 03:26 PM

die("Connection not established"); isn't an error message thrown by PHP, so display_errors() would not affect it, as it is user-displayed information followed by an order to halt script execution. Also, when you turn display_errors off, you should make sure log_errors is on, and that error_log is set to a file name you would know to look in for errors. Otherwise, you'll never get the errors your site throws and will thus be unable to debug them.

This post has been edited by Dormilich: 21 February 2011 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1