Erik the Hack
Here's the scoop: the PHP code that my web site uses to perform MySQL queries is very old (it was inherited from some other code), and it is now causing me problems. Every once in a while I will now get the following error:
User has exceeded the 'max_questions' resource
All of my queries go through a Database class object, via the following member functions:
/* ---------------------------------------------------------- */
# Connect to the database
function connect()
{
$this->Handle = @mysql_pconnect($this->Host, $this->User, $this->Password);
if(!$this->Handle)
{ $this->report_server_error("Connect to database failed"); }
$this->debug_output("Successfully connected to database: " . $this->Name);
return mysql_select_db($this->Name);
}
/* ---------------------------------------------------------- */
# Execute query
function query($Query)
{
# Attempt the query at least 10 times:
for($Attempt = 1; $Attempt <= 10; $Attempt++)
{
$Result = mysql_query($Query);
if($Result)
{
return $Result;
}
sleep(1);
}
# The query could not be executed, so display an error:
$this->report_server_error("Query failed");
}
/* ---------------------------------------------------------- */
# Disconnect from the database
function disconnect()
{
@mysql_close($this->Handle);
}
/* ---------------------------------------------------------- */
So, basically, the way it works is I first call Database::connect() to get my connection, I perform queries via Database::query(), and then I close the connection via Database::disconnect(). As you can see, the connect() function uses the old @mysql_pconnect() call, which is bad. I assume that's why I sometimes get the error above.
Anyway, I would like to go through and re-write this code using the @mysql_real_connect() function call, and I wanted to get some opinions here first. I'm no MySQL expert myself, so I wanted to insure I choose the optimal solution. So, how would you replace this code? :)
Thanks!
User has exceeded the 'max_questions' resource
All of my queries go through a Database class object, via the following member functions:
/* ---------------------------------------------------------- */
# Connect to the database
function connect()
{
$this->Handle = @mysql_pconnect($this->Host, $this->User, $this->Password);
if(!$this->Handle)
{ $this->report_server_error("Connect to database failed"); }
$this->debug_output("Successfully connected to database: " . $this->Name);
return mysql_select_db($this->Name);
}
/* ---------------------------------------------------------- */
# Execute query
function query($Query)
{
# Attempt the query at least 10 times:
for($Attempt = 1; $Attempt <= 10; $Attempt++)
{
$Result = mysql_query($Query);
if($Result)
{
return $Result;
}
sleep(1);
}
# The query could not be executed, so display an error:
$this->report_server_error("Query failed");
}
/* ---------------------------------------------------------- */
# Disconnect from the database
function disconnect()
{
@mysql_close($this->Handle);
}
/* ---------------------------------------------------------- */
So, basically, the way it works is I first call Database::connect() to get my connection, I perform queries via Database::query(), and then I close the connection via Database::disconnect(). As you can see, the connect() function uses the old @mysql_pconnect() call, which is bad. I assume that's why I sometimes get the error above.
Anyway, I would like to go through and re-write this code using the @mysql_real_connect() function call, and I wanted to get some opinions here first. I'm no MySQL expert myself, so I wanted to insure I choose the optimal solution. So, how would you replace this code? :)
Thanks!