php mysql_real_escape_string函數用法與執行個體教程

來源:互聯網
上載者:User

mysql教程_real_escape_string - 轉義特殊字元的SQL語句中使用字串

string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )

 

mysql_real_escape_string()調用MySQL的庫函數mysql_real_escape_string,其中prepends反斜線以下字元: x00 ñ ṛ,,',“和但X1a。

這個函數必須始終(除了少數例外)被用來製作發送之前查詢到MySQL資料的安全。

unescaped_string
該字串進行轉義。

link_identifier
MySQL的串連。如果沒有指定串連標識符,最後一個環節開幕mysql_connect()函數假設。如果沒有這樣的連結被發現,它會嘗試建立一個猶如mysql_connect()是不帶參數調用。如果沒有找到或建立串連,一會產生一條E_WARNING層級的錯誤。

執行個體一

<?php教程
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());

// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));
?>

執行個體二

<?php
// Query database to check if there are any matching users
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);

// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";

// This means the query sent to MySQL would be:
echo $query;
?>
查詢發送到MySQL:

選擇*從使用者的WHERE使用者='艾丹'和password = ''或''=''
這將允許任何人登入沒有有效密碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.