php mysql_real_escape_string()函數_PHP教程

來源:互聯網
上載者:User
mysql教程_real_escape_string() 函數轉義 SQL 陳述式中使用的字串中的特殊字元。

下列字元受影響:

x00
n
r

'
"
x1a
如果成功,則該函數返回被轉義的字串。如果失敗,則返回 false。

文法
mysql_real_escape_string(string,connection)參數 描述
string 必需。規定要轉義的字串。
connection 可選。規定 MySQL 串連。如果未規定,則使用上一個串連。
執行個體

function opendatabase ($host,$user,$pass) {
try {
if ($db = mysql_connect ($host,$user,$pass)){
return $db;
} else {
throw new exception ("Sorry, could 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 could 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);

?>

http://www.bkjia.com/PHPjc/630752.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630752.htmlTechArticlemysql教程_real_escape_string() 函數轉義 SQL 陳述式中使用的字串中的特殊字元。 下列字元受影響: x00 n r ' x1a 如果成功,則該函數返回被轉義的...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.