kjtocool Posted December 12, 2007 Share Posted December 12, 2007 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? Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 Are you sure it is actually connecting? You aren't getting the "Unable to connect to the database" message, right? Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 12, 2007 Author Share Posted December 12, 2007 Nope, not getting the "Unable to connect to the database." message, thus why I'm so confused. Quote Link to comment Share on other sites More sharing options...
atticus Posted December 12, 2007 Share Posted December 12, 2007 shouldn't Line 170 is: $input = mysqli_real_escape_string($databaseConnect, $input); be: Line 170 is: $input = mysql_real_escape_string($databaseConnect, $input); Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 Why would he use that when he is connecting with msqli? $databaseConnect should be a mysqli db resource, not a mysql one. Quote Link to comment Share on other sites More sharing options...
atticus Posted December 12, 2007 Share Posted December 12, 2007 I just looked at this line: $input = mysqli_real_escape_string($databaseConnect, $input); my bad...I have never used a mssql server...I treat ms like the plague Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 12, 2007 Author Share Posted December 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted December 12, 2007 Share Posted December 12, 2007 $databaseConnect does not exist within your function. You need to pass it in as an argument. Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 12, 2007 Author Share Posted December 12, 2007 $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? Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 12, 2007 Author Share Posted December 12, 2007 Never mind, i'm just an idiot who left the login and password and database as: "username", "password", "database" Once I changed them, it worked fine. You were right about the visibility being the issue, much thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.