Jump to content

mysqli_real_escape_string() error


galvin

Recommended Posts

I am updating all my code from mysql to mysqli.  Currently using PHP 5.4 but will update to 5.5 once all this updating is done.

 

Anyway, I have this old function for making data safe for inserting into mysql database.  I changed all instances of "mysql" to "mysqli"...

	function mysqli_prep($value) {
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists("mysqli_real_escape_string") ; //i.e. PHP >= v4.3.0
		if($new_enough_php) { //PHP v4.3.0 or higher
			//undo any magic quote effects so mysqli_real_escape_string can do the work
			if($magic_quotes_active) { $value = stripslashes($value) ;}
			$value = mysqli_real_escape_string($connection, $value);
		} else { //before php v4.3.0
			// if magic quotes aren;t already on then add slashes manually
			if(!magic_quotes_active) { $value = addslashes($value); }
			// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}

When I load that page that calls this function, I get...

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in (mypath)

This is my $connection by the way, which works fine on other pages that need it...

$connection = mysqli_connect('localhost', 'myusername', 'mypassword', 'mytable');
	if (!$connection) {
		die("database connection failed: " . mysqli_error());
	}

Any ideas what I'm doing wrong?

Link to comment
Share on other sites

  • 3 months later...

It's just what Ch0cu3r said, the mysqli_prep() function had no knowledge of the $connection variable, as it was defined outside of the function but not passed to the function where it was used. Ch0cu3r also posted links explaining variable scope.

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.