Jump to content

Problems with function and mysqli_real_escape_string


pioneerx01
Go to solution Solved by pioneerx01,

Recommended Posts

I am starting to work with MySQLi and so far so good. I am having issues with mysqli_real_escape_string in function.

	if(!function_exists('todatabase'))
	{
		function todatabase ($variable)
		{
			$variable = mysqli_real_escape_string($variable);
			return $variable;
		}
	}

Every time I put something into database I run it through "todatabase" function, but if I have mysqli_real_escape_string in there the function does not execute. I also get no error messages and rest of the code runs to the end smooothly. I tried using:

 

$variable mysqli_real_escape_string($dbc, $variable);

$variable = $dbc->real_escape_string($variable);

 

But it did not work either. What am I missing?

Edited by pioneerx01
Link to comment
Share on other sites

The connect code is

	DEFINE ('DBNAME', '');
	DEFINE ('DBUSER', '');
	DEFINE ('DBHOST', '');
	DEFINE ('DBPW', '');
	
	$dbc = mysqli_connect(DBHOST,DBUSER,DBPW,DBNAME);
	
	if (mysqli_connect_errno($con))
	{
		echo "Could not connect to the database. <br/>";
		exit();
		die();
	}
	
	else
	{
		mysqli_set_charset($dbc, "utf8");
	}

When I do this

$dbc->query("UPDATE table SET `first_name` = '".todatabase($first_name)."' WHERE ID = '#' ");

first_name remains the same and does not change, no errors though. (errors are not supressed)

Edited by pioneerx01
Link to comment
Share on other sites

you need to pass the $dbc variable into the function as a call time parameter.

 

you also need to turn on php's error_reporting/display_errors to get php to help you. you would be getting several php error's when you tried to use $dbc inside the function.

 

lastly, for a user written function, there's no good reason to test if it doesn't exist before defining it. that's just more lines of code to clutter up what you are doing.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

Oh yes, I could do just that. Why did't I think of that:

 

    function todatabase ($dbc, $variable)
    {
        $variable = preg_replace('/\s+/', ' ', $variable);
        $variable = mysqli_real_escape_string($dbc, $variable);
        return $variable;
    }
 

 

$dbc->query("UPDATE table SET `first_name` = '".todatabase($dbc, $first_name)."' WHERE ID = '#' ");

 

Thank you all.

Edited by pioneerx01
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.