Phpmysql_real_escape_string () function _ PHP Tutorial

Source: Internet
Author: User
Phpmysql_real_escape_string () function. Mysql tutorial _ real_escape_string () function escape special characters in strings used in SQL statements. The following characters are affected: if x00nrx1a succeeds, the function returns the special characters in the character strings used in the escape mysql tutorial _ real_escape_string () function to escape SQL statements.

The following characters are affected:

X00
N
R

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

Syntax
Mysql_real_escape_string (string, connection) parameter description
String is required. Specifies the string to be escaped.
Connection is optional. MySQL connection is required. If not specified, use the previous connection.
Instance

Function opendatabase ($ host, $ user, $ pass ){
Try {
If ($ db = mysql_connect ($ host, $ user, $ pass )){
Return $ db;
} Else {
Throw new exception ("Sorry, cocould not connect to mysql .");
}
} Catch (exception $ e ){
Echo $ e-> getmessage ();
}
}

Function selectdb ($ whichdb, $ db ){
Try {
If (! Mysql_select_db ($ whichdb, $ db )){
Throw new exception ("Sorry, database cocould not be opened .");
}
} Catch (exception $ e ){
Echo $ e-> getmessage ();
}
}
Function closedatabase ($ db ){
Mysql_close ($ db );
}
$ Db = opendatabase ("localhost", "root ","");
Selectdb ("mydatabase", $ db );
$ _ POST ['user'] = "myname ";
$ _ POST ['pass'] = "mypassword ";

Function validatelogin ($ user, $ pass ){
Mysql_real_escape_string ($ user );
Mysql_real_escape_string ($ pass );
$ Thequery = "SELECT * FROM userlogin WHERE username = '$ user' AND password =' $ pass '";
If ($ aquery = mysql_query ($ thequery )){
If (mysql_num_rows ($ aquery)> 0 ){
Return true;
} Else {
Return false;
}
} Else {
Echo mysql_error ();
}
}

If (validatelogin ($ _ POST ['user'], $ _ POST ['pass']) {
Echo "You have successfully logged in .";
} Else {
Echo "Sorry, you have an incorrect username and/or password .";
}

Closedatabase ($ db );

?>

The Escape () function escapes special characters in strings used in SQL statements. The following characters are affected: if x00 n r'x1a succeeds, the function returns the escaped...

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.