Jump to content

Mysql-link resource goes bad mid-script?


linus72982

Recommended Posts

Okay, I have a section of code to delete a message in a private message mailbox-like script.  Before this section of the script fires, I use $this->_database-> many times to pull the names of the messages, etc - that all works fine, but when my program gets to the delete section of my database class, I get the following error:

 

   

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user '*private*'@'localhost' (using password: NO) in /var/www/html/evoHTDOCS/tinUser_database.php on line 25
    
    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /var/www/html/evoHTDOCS/tinUser_database.php on line 25
    
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/evoHTDOCS/tinUser_database.php on line 210
    Access denied for user '*private*'@'localhost' (using password: NO)ERROR2

 

Disregard the "ERROR2" at the end, that's just the catch for a non-result and it tells me which part of the script went wrong.  Anyway - I use the same link-resource ($this->connection) many times in this same instance of this same class without error - but for some reason this one brings up that error.  Also, what's odd to me is that in the error messages the username is different than the username that I am using to login (they are constants defined in a config file) - it's using the primary username instead of mine and yes, I checked the permissions and they are all set (and again, it links just fine with the same information many times before this function.)  It sounds like all the sudden it says it can't connect to the database it has proved it was already connected to.

 

If you need it, here is the database section that starts the link and then the function that is causing the error:

 

    public function __construct() {
	$this->connection = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
	mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

    public function updatePMUser($field, $data, $whereField, $whereData) {
	$data = $this->escapeString($data);
	$query = "UPDATE ".PM_USERS_TABLE." SET ".$field." = '$data' WHERE ".$whereField." = '$whereData'";
	$result = mysql_query($query, $this->connection);
	if ($result)
		return $result;
	else {
		echo mysql_error();
		die("ERROR2");
		}
	}

 

It's all still in production so yes there is some trimming to do (and making the connection static, etc), but what could be causing the exact same connection to suddenly fail at a specific function?  I've checked and rechecked spelling and all that.  Thanks for any help.

Link to comment
Share on other sites

The error indicates it is a call to mysql_real_escape_string() that is causing the error. This function requires a connection to the database. The mysql_* functions will use the last connection that was established if one is not specified. And if there have been no connections, they will try to connect as the current user with no password.

 

Take a look at your mysql_real_escape_string() calls. The error message says LINE 25, so start there and look above and below it.  It looks like your method name is escapeString() it looks like that call, two lines above the mysql_query() call, is failing, causing the UPDATE query to embed some garbage data which is causing that last error message.

 

 

Link to comment
Share on other sites

I figured it out.  The escapeString function was fine as I suspected as it was being called many times before this point without issue.  The problem was that the function being called at the point in question works off of an unserialized version of the object - and I didn't realize serialization destroys connections.  I had to add a __wakeup function that restores the connection variable after unserialization.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.