1

I get "Notice: Undefined variable: con in C:\wamp\www\Game\functions.php on line 8" when trying to use the function, here's the code.

function protect($string) {
return mysqli_real_escape_string($con,strip_tags(addslashes($string)));
}

I use the $con for my queries and it's fine so I thought that was what was for this mysqli part?

This is for registration, I have some registration that is working but I can't use that, here's a confirmed working line

$res=mysqli_query($con,$sql);

Any ideas?

4
  • mysqli_real_escape_string needs a connection BEFORE it can function,use global $con; as the first line in your function. Commented Sep 16, 2014 at 17:34
  • 1
    Using just "global $con;" worked, thanks! Commented Sep 16, 2014 at 17:47
  • @mihai How do I choose it as answer? lol Commented Sep 16, 2014 at 17:47
  • Post it as your own answer and accept it after a couple of days. Commented Sep 16, 2014 at 17:48

2 Answers 2

2

$con doesn't exist in the function protect(), so you either need to make $con global:

global $con = mysqli_connect();

or you need to pass $con as an argument:

function protect($string, $con) {
    return mysqli_real_escape_string($con,strip_tags(addslashes($string)));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have "$con=mysqli_connect("localhost","root","");" in a connect.php file with "Include('connect.php');" but still doesn't work :(
2

ANSWER FROM MIHAI

mysqli_real_escape_string needs a connection BEFORE it can function,use global $con; as the first line in your function. – Mihai

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.