Should I use mysqli_real_escape_string or should I use prepared statements? [duplicate]
Asked Answered
T

3

8

Should I use mysqli_real_escape_string or should I use prepared statements?

I've seen a tutorial now explaining prepared statements but I've seen them do the same thing as mysqli_real_escape_string but it uses more lines

Are there any benefits for prepared statements? What do you think is the best method to use?

Tang answered 3/4, 2013 at 11:36 Comment(4)
Use prepared statements.Modeling
#2354166Menchaca
I think this will fix your doubts, #60674 ThanksGuddle
If you're looking for performance you should use real_escape_string instead of prepared statement because is twice faster. Details here: jimwestergren.com/pdo-versus-mysqliTophet
A
6

Prepared statements only. Because nowhere escaping is the same thing. In fact, escaping has absolutely nothing to do with whatever injections, and shouldn't be used for protection.

While prepared statements offer the 100% security when applicable.

Allwein answered 3/4, 2013 at 11:45 Comment(4)
How is it possible to inject with the latter? Given UTF-8 and that using mysql_real_escape_string of course implies using delimiters or int casts as well.Maidel
If you're looking for performance you should use real_escape_string instead of prepared statement because is twice faster. Details here: jimwestergren.com/pdo-versus-mysqliTophet
@Tophet First, it says that PDO offers performance without penalty. Second and most important, do not trade security for the performance, you'll lose both. Nobody cares whether your hacked site is fast.Allwein
My comment is related to the question. Your answer haven't addressed the performance domain. But I agree with you, security first, performance later.Tophet
S
5

Use prepared statements because after using this you doesn't have to use mysqli_real_escape_string. prepared statements doing this as by default.

Siskind answered 3/4, 2013 at 11:39 Comment(4)
Because ...do the same thing as mysqli_real_escape_string but it uses more lines... OP wrote it in question. P.S. downvote not from me ;)Pander
Well, I know I don't have to use mysqli_real_escape_string after using a prepared statement, but why shouldn't I just use mysqli_real_escape_string from the beginning and not use a prepared statement?Tang
"prepared statements doing this as by default." Not true. Prepared statement (with no emulation) has other mechanizm.Dumm
"because you doesn't have to use mysqli_real_escape_string". yes. it rather makes you to use A LOT MORE functions to run single query. That's the point of the question: how to write less code, not more.Allwein
M
0

It's very easy to forget (maybe not for you, but other developers you work with) to escape whereas it's very hard to use prepared statements incorrectly to cause a vulnerability. So prepared statements.

Maidel answered 3/4, 2013 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.