linus72982 Posted April 7, 2011 Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232928-mysql-link-resource-goes-bad-mid-script/ Share on other sites More sharing options...
DavidAM Posted April 7, 2011 Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232928-mysql-link-resource-goes-bad-mid-script/#findComment-1198005 Share on other sites More sharing options...
linus72982 Posted April 7, 2011 Author Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232928-mysql-link-resource-goes-bad-mid-script/#findComment-1198055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.