I'm trying to work a bit of security and sanitization into my databases application (for a class). to start off with, i'm trying to use mysql_real_escape_string, but whenever i use it, it always returns an empty string!
Here's the connection code:
include_once ("./connect.php");
$db_connection = new mysqli($SERVER, $USERNAME, $PASSWORD, $DATABASE);
if (mysqli_connect_errno()) {
echo("Can't connect to MySQL Server. Error code: " . mysqli_connect_error());
return null;
}
$field = mysql_real_escape_string($_GET['value']);
$upc = $_GET['upc'];
$type = $_GET['field_type'];
echo $field;
echo $upc;
echo $type;
When the php actually gets executed, the $upc and $type gets printed, but NOTHING for $field. Ive tried using an intermediate string, but i get the same result. I'm seriously at a loss as to what it is thats going wrong here.
Also, I've done a var_dump on $field
, and it claims mysql_real_escape_string
returns FALSE
, which is supposed to happen when there isn't a connection(?), but there is one.
You are connecting using mysqli, not mysql, so you need mysqli_real_escape_string
Although you could easily use prepared statements in mysqli and then you wouldn't need either...
See PHP Documentation http://php.net/manual/en/function.mysql-real-escape-string.php
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.
Which Means you need an active connection to use this function
If you are not seeing any error try
error_reporting(E_ALL);
ini_set('display_errors','On');
mysql_real_escape_string
alternative
Use
$escaped = htmlspecialchars($_GET['value'], ENT_QUOTES, "ISO-8859-1");
OR
$escaped = filter_var($_GET['value'], FILTER_SANITIZE_STRING);
You're getting any empty string because the function takes two parameters; the actual connection to the database and then the string. Use this:
$sanitizedField = mysqli_real_escape_string($connection, $field);
"This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used." from http://php.net/manual/en/function.mysql-real-escape-string.php
Simply adding "i" to the function name is not the solution. Notice the link identifier parameter is no longer optional http://www.php.net/manual/en/mysqli.real-escape-string.php
PS: As far as the downvotes, I stand by my answer. The function in the question is going to be removed from PHP, that's a big deal and I felt it needed to be pointed out since nobody else mentioned it. Do you disagree? The specifics of the original question are important but many people (including myself) came here looking for information on mysql_real_escape_string() and the first thing you should realize when looking for information about mysql_real_escape_string() is that it's deprecated. Sysadmins will have to upgrade PHP sooner or later and no doubt a ton of applications are still using this deprecated function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With