: Need opinions on MySQL code upgrade


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!

Erik the Hack
Anyone? Bueller? Bueller?

My first attempt to replace the connection call with @mysql_real_connect() failed miserably, and I'm not sure what's going wrong. Any advice or comments would be greatly appreciated!

Litazia Tanxashira
mysql_real_connect()?...

I don't see such a thing in the PHP documentation... do you mean mysql_connect()?

Erik the Hack
On the MySQL web site, they said that @mysql_connect() has now been replaced by @mysql_real_connect(). I suppose I was just trying to be as up-to-date as possible... :)

Litazia Tanxashira
Ok, I'm confused... are you going to the PHP site for MySQL connecting help, or the MySQL site? :D

Erik the Hack
Both, actually! :D