Jump to content

[SOLVED] msqli_real_escape_string is giving me an error


kjtocool

Recommended Posts

I'm using the following code:

 

<?php
$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
	Or die("Unable to connect to the database.");

function test_input($input) {
	// Stripslashes
	if (get_magic_quotes_gpc()) {
	  $input = stripslashes($input);
	}

	// Escape if not a number
	if (!is_numeric($input)) {
	  $input = mysqli_real_escape_string($databaseConnect, $input);
	}

	return $input;
}

$user = test_input($_POST['userName']);
echo $user;
?>

 

And getting the following error:

 

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/worldofk/public_html/CMS/loginVal.php on line 170

 

Line 170 is: $input = mysqli_real_escape_string($databaseConnect, $input);

 

Why am I getting yelled at?

Link to comment
Share on other sites

I thought I'd try something else, and see what it threw at me.  So I switched the code to:

 

<?php
$userName = $_POST["userName"];
$passwordHash = md5($_POST["password"]);

$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
Or die("Unable to connect to the database.");

if($databaseConnect)
echo "win";
else
echo "probably an error";

$query = "INSERT INTO Admin_Users VALUES(NULL, '$userName', '$passwordHash', 1";

if (mysqli_query($databaseConnect, $query))
echo "New user successfully created.";
else
echo "Unable to create new user.";

mysqli_close($databaseConnect);
?>

 

I got: winUnable to create new user.

 

Maybe there is something wrong with the connection, but I just can't see what it could be.

Link to comment
Share on other sites

$databaseConnect does not exist within your function. You need to pass it in as an argument.

 

Interesting, I had not thought of that.  I assumed that it would have access to the variable since it was 'outside' the function.

 

I tried what you suggested (though I simply put it inside the function, in the future I would do as you suggest and pass it as a parameter).  Now I'm getting a connection error, so something is wrong with my connect statement, but I don't understand what it could be.

 

<?php
function test_input($input) {
$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
	Or die("Unable to connect to the database.");

// Stripslashes
if (get_magic_quotes_gpc()) {
  $input = stripslashes($input);
}

// Escape if not a number
if (!is_numeric($input)) {
  $input = mysqli_real_escape_string($databaseConnect, $input);
}

return $input;
}

$user = test_input($_POST['userName']);
echo $user;
?>

 

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'username'@'localhost' (using password: YES) in /home/worldofk/public_html/CMS/loginVal.php on line 160
Unable to connect to the database.

 

Is something wrong with the host variable of my mysqli_connect statement?

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.