$username=addslashes($username);

mysql_escape_string($string):用反斜杠转义字符串中的分外字符,用于mysql_query()查询。

$username=mysql_escape_string($username);

php防止sqlphp防止sql注入 RESTful API

mysql_real_escape_string($string):转义SQL语句中利用的字符串中的分外字符,并考虑到连接确当前字符集,须要担保当前是连接状态才能用该函数,否则会报警告。
不转义%与_

$username=mysql_real_escape_string($username);

两种选择:

利用PDO

$stmt = $pdo->prepare('SELECT FROM user WHERE name = :name');

$stmt->execute(array(':name' => $name));

foreach ($stmt as $row) {

// do something with $row

}

利用mysqli

$stmt = $dbConnection->prepare('SELECT FROM user WHERE name = ?');

$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {

// do something with $row

}