PHP MySQL Query

By TimRoden on Jul 09, 2011

Simple MySQL query that auto connects to a server if connection has timed out, or isn't there to begin with. Use in place of mysql_query.

Example:
$result = my_mysql_query("SELECT * FROM foo.bar");

In the code, we see SQL_SERVER, SQL_USER, SQL_PASS, and SQL_DB. These are defined using the define(); function.

Example:
define('SQL_SERVER','localhost');

function my_mysql_query($query, $link_identifier = false)
{
    if (!mysql_ping()) {
        mysql_close();
        mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
        mysql_select_db(SQL_DB);
    }
    if ($link_identifier === false)
        return mysql_query($query);
    else
        return mysql_query($query, $link_identifier);   
}

Comments

Sign in to comment.
PuNkTuReD   -  Jul 10, 2011

definitely a handy function, thanks for the share...

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.