Excuse me, why did MySQL preprocessing prevent SQL injection?

Source: Internet
Author: User
Ask, why is it that MySQL preprocessing can prevent SQL injection???
Like what:
PHP Code
  
     $link = new mysqli (' localhost ', ' root ', ' 111111 ', ' test ');  $link-Set_charset (' UTF8 ');  $sql = ' SELECT * from Test WHERE id=? and name=? ';  $stmt = $link-Prepare ($sql);  $stmt-Bind_param (' is ', $id, $name);  $stmt, execute ();


For the above example, my understanding is:
MySQL has already killed the SQL statement "?" before it actually executes. The type value that the number represents.
So, if you give the ID at the location of the "? "Passes a string value, it also turns into an integer, which prevents SQL injection.

However, the name of the "? ", the string should have been passed, and if it was passed to a SQL injected statement, how can you prevent SQL injection?"

Do not know my understanding is right, please advise! Thank you!!

------Solution--------------------
Addslashes ()
------Solution--------------------
The implementation of the prepare preprocessing is essentially an internal escape process, escaping some characters that need to be escaped, and you can't see it, just as you encapsulate an escape method similar, if it is executed directly without preprocessing, there is no escaping this step, thus increasing the risk factor
------Solution--------------------
discuss

Reference:

The implementation of the prepare preprocessing is essentially an internal escape process, escaping some characters that need to be escaped, and you can't see it, just as you encapsulate an escape method similar, if it is executed directly without preprocessing, there is no escaping this step, thus increasing the risk factor

Is it like upstairs yes with a addslashes (), or mysql_escape_string ()?

------Solution--------------------
Develop the habit, do not understand the question asked Php.net, really can not find again to ask the forum.

The conclusion is that you do not need to addslashes, your SQL statement, or "surrounded, prepare automatic addlashes ()."
------Solution--------------------
$v = (Is_int ($v)? $v: "'". Pg_escape_string ($v). "'";
LZ and I use the same is used? Placeholder mode.

? corresponding to the value one by one, before the query is replaced, this time the shape of the Is_int, the string is used Pg_escape_string ()
Pg_escape_string () escapes a string of Text/char data type, returning the escaped string. It is recommended that you replace addslashes () with this function. (I use the Pgsql database) to escape.

$real _sql. = Preg_replace ('/\?/', $v, $sql _arr[$key],1); This is? The replacement.
------Solution--------------------
Mysql_escape_string Use this to accept the user or write their own users of the value of the user is basically to prevent SQL injection
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.