1

When I am inserting into a database with mysqli_real_escape_string, I am finding that my single quotes are been escaped with \\ rather than \ which is causing my query to fail. See below:

NOTE: $link is my db connection var.

$string = mysqli_real_escape_string($link, "BEGIN testing quotes - don't use quotes END");
$query = "INSERT INTO table (field) VALUES ('".$string."')";

When I echo out my query, I get:

INSERT INTO table (field) VALUES ('BEGIN testing quotes - don\\'t use quotes END')

which is causing a SQL syntax error. I cannot seem to find a setting anywhere that can change this. If I copy the echo'd query into MySQL workbench and remove a \, the query insert's perfectly.

I have had a look through Stack Overflow and cannot find anything relating to this, and also searched through Google with no luck.

I have many queries that need escaping across my entire website. Could a setting be set to automatically apply escaping of strings pre-insert without having to go through and update all my variables? If not, Is there anyway I can alter the mysqli_real_escape_string function without having to manually check every string I insert for single quotes etc?

I appreciate any assistance with this.

5
  • 4
    use prepared statements Commented Dec 18, 2015 at 10:52
  • don't. Use prepared statements instead look at this Commented Dec 18, 2015 at 10:52
  • 2
    Sounds like you have magic quotes turned on. Don't do that. And do use prepared statements instead. Commented Dec 18, 2015 at 11:03
  • Magic Quotes are disabled, checked that one. Stripslashes worked. Commented Dec 18, 2015 at 11:19
  • $string = mysqli_real_escape_string($link, stripslashes("BEGIN testing quotes - don't use quotes END")); Commented Dec 18, 2015 at 11:20

1 Answer 1

-2

As Krishna Gupta suggested, stripslashes resolved my issue:

$string = mysqli_real_escape_string($link, stripslashes("BEGIN testing quotes - don't use quotes END"));

Thanks.

Sign up to request clarification or add additional context in comments.

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.