Mysql inserts a string with single quotes and double quotation marks

Source: Internet
Author: User
When a user submits a form, some users submit some special characters, such as single quotation marks and double quotation marks. if you insert data directly to the database based on the normal string, the database may not be correctly inserted. For special characters in the form, php provides a function mysql_real_escape_string, which can escape special characters. Mysql_real_escape_string ()Special characters in strings used in function escape SQL statements.

If yes, the function returns the escaped string. If it fails, false is returned.

SyntaxMysql_real_escape_string (string, connection)

Parameters Description
String Required. Specifies the string to be escaped.
Connection Optional. MySQL connection is required. If not specified, use the previous connection.

For example, we want to insert the string "my name's zhangsan" into the database. if you write an SQL statement as follows, an error is returned.

$str="my name's zhangsan";mysql_query("insert into table (field1) values ('".$str."')");

In this case, an error is reported. The single quotes in the string conflict with the SQL single quotes.

Which of the following statements is true?

// Escape the string to use $ str = mysql_real_escape_string ("my name's zhangsan"); mysql_query ("insert into table (field1) values ('". $ str. "')");

In this case, the string stored in the database is "my name/'s zhangsan". to tell the truth about this string, you need to use stripslashes () after reading the data () the function removes the backslash.

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.