mysql_real_escape_string(string,connection)函数的作用是:转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集。如果函数执行成功,将返回转义后的字符串;如果执行失败将返回False。
mysqll_escape_string()函数的作用是:转义 SQL 语句中使用的字符串中的特殊字符,不考虑连接的当前字符集。
复制内容到剪贴板
代码:
<?php
function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (!is_numeric($value))
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Make a safe SQL
$user = check_input($_POST['user']);
$pwd = check_input($_POST['pwd']);
$sql = "SELECT * FROM users WHERE
user=$user AND password=$pwd";mysql_query($sql);mysql_close($con);
?>[
本帖最后由 极品黑公子 于 2007-8-23 13:34 编辑 ]