views:

46

answers:

3

So, I'm getting this warning when using mysql_real_escape_string

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'username'@'localhost' (using password: NO) in /home/path/php/functions.php on line 11

The rest of the site works fine, connects to the DB and all, but I get an error when using this function.

It works completely fine on my localhost testing server.

Any ideas?

I use aforementioned function in my own homebrew string sanitation function:

function sani($string){     
  $string = strip_tags($string); 
  $string = htmlspecialchars($string); 
  $string = trim(rtrim(ltrim($string))); 
  $string = mysql_real_escape_string($string);
  return $string;
}

And I use this function every time I do queries...

function mm_mysqlquery($query) {
 if (MM_DEBUG == true) { echo "Query: $query <br>"; mm_log("Query: $query"); } //print query if MM_DEBUG
 $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die ("mysql_error: " . mysql_error());
 $db_selected = mysql_select_db(DB_NAME);
 return mysql_query($query, $link);
}

Thanks on beforehand!

A: 

Are you sure you have a connection open when you call the function? If yes, I don't know the cause of the warning, but you can use addslashes() instead. It does the same thing without an extra DB call.

Claudiu
Turns out I wasn't sure about that... Now I do the mysql_connect outside the function, and use global to get the $link variable in there!
Simon
Whatever suits you :) I ussually call the connection code in my config file so I never run into problems like this. Glad you sorted it out!
Claudiu
don't use globals - pass the $link variable to the function as an argument by reference: `mm_mysqlquery($query, `
Clay Hinson
do **NOT** use `addslashes()` to sanitize sql data
knittl
Easy there @knittl. Adding slashes isn't the only sanitization he does in his function. Do you know of any cases that he could miss with sanititization function by just adding slashes instead of using mysql_real_escape_string()? Yeah, sure mysql_real_escape string is recomenended, but addslashes() is provided as a general solution, it won't escape everthing, but then again, what's left to escape except what he already does?
Claudiu
mysql_real_escape_string also takes care of the input encoding. he uses other functions as well, but you said he can use addslashes _instead_ – which just makes me cramp
knittl
+1  A: 

First point: If you're getting an error from mysql_real_escape_string(), it's because you are calling the function before you're connected to the database.

It looks like you connect to the database right before you run a query. So anything you do before you call your mm_mysqlquery() function won't have a connection.

The mysql_real_escape_string() function needs a live connection to the database, so it can do the right kind of escaping with respect to the connection's character set. So you need to connect before you do escaping.

It's better to do that anyway, because if you make several queries during the course of a single PHP request, it's less overhead to connect once and use the same connection for all your queries.

Second, please don't take suggestions to use addslashes() -- it does not do the same thing as mysql_real_escape_string(). The two are not interchangeable. You should get into the habit of using mysql_real_escape_string().

Third, your sani() function shows a common misconception.

function sani($string){     
  $string = strip_tags($string); 
  $string = htmlspecialchars($string); 
  $string = trim(rtrim(ltrim($string))); 
  $string = mysql_real_escape_string($string);
  return $string;
}

The common misconception is that you need all these functions to make a string safe in an SQL statement. You don't. Only mysql_real_escape_string() is necessary. All the other functions in this example do nothing to protect against SQL injection.

Those functions are useful if you output a string in an HTML presentation and you want to reduce the risk of XSS attacks, but then mysql_real_escape_string() is irrelevant.

Use each type of sanitizing method in its appropriate context.

Bill Karwin
A: 

string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )

Where $unescaped_string is your string and $link_identifier is your db resource.

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

PHP.NET mysql_real_escape_string resource

1900TDiDi