MySQL Forums
Forum List  »  PHP

Re: mysql_connect() error
Posted by: Philip Olson
Date: June 12, 2014 11:44PM

Also add error checking/handling to your code. For example, do not attempt to select a database if the database connection failed. And do not attempt to execute a query if the the database selection failed.

In other words:

$conn = mysql_connect(...);
if (!$conn) {
    // Do something here that does not require a database, like mention
    // the site is down for maintenance... :) Or simply exit.
    exit;
}
if (!mysql_select_db(...)) {
    // Same as a failed connection. This is not good. But considering the
    // database cannot be selected, should a query be attempted? Nope.
    exit;
}

$sql = "SELECT foo FROM bar";
$res = mysql_query($sql);
if (!$res) {
    // You get the idea. If the query failed then do something
    // about that. If you always do something then you control
    // what happens.
    // Or maybe the query succeeds but returns zero rows. Are
    // you handling zero rows? 1000 rows? And so on.
}
Maybe that will help, although I may be misreading the question. Lastly, consider moving from ext/mysql to ext/mysqli or PDO_MySQL soon.

---
Philip Olson
Technical Writer
MySQL Documentation Team @ Oracle

Options: ReplyQuote


Subject
Written By
Posted
June 10, 2014 12:47PM
June 10, 2014 10:32PM
Re: mysql_connect() error
June 12, 2014 11:44PM
June 13, 2014 04:15PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.