mysql_escape_string

(PHP 4 >= 4.0.3, PHP 5)

mysql_escape_stringmysql_query で使用するために文字列をエスケープする

警告

この関数は PHP 4.3.0 で非推奨になり、PHP 7.0.0 で MySQL 拡張モジュール 全体とあわせて削除されました。 MySQLi あるいは PDO_MySQL を使うべきです。詳細な情報は MySQL: API の選択 を参照ください。 この関数の代替として、これらが使えます。

説明

mysql_escape_string(string $unescaped_string): string

この関数は、mysql_query() で指定可能なように unescaped_string をエスケープします。 この関数は非推奨です。

この関数は mysql_real_escape_string() とほぼ同じです。ただ mysql_real_escape_string() はコネクションハンドラを用い、 カレントの文字セットを考慮したエスケープを行うという点が違います。 mysql_escape_string() はコネクションに関する引数を 持たず、カレントの文字セット設定を考慮しません。

パラメータ

unescaped_string

エスケープされる文字列。

返り値

エスケープされた文字列を返します。

例1 mysql_escape_string() の例

<?php
$item 
"Zak's Laptop";
$escaped_item mysql_escape_string($item);
printf("Escaped string: %s\n"$escaped_item);
?>

上の例の出力は以下となります。

Escaped string: Zak\'s Laptop

注意

注意:

mysql_escape_string() は、 % および _ をエスケープしません。

参考

add a note add a note

User Contributed Notes 2 notes

up
8
PHPguru
9 years ago
You can use this function safely with your MySQL database queries if and only if you are sure that your database connection is using ASCII, UTF-8, or ISO-8859-* and that the backslash is your database's escape character. If you're not sure, then use mysqli_real_escape_string instead. This function is not safe to use on databases with multi-byte character sets.

The only benefit of this function is that it does not require a database connection.
up
-25
s dot marechal at jejik dot com
13 years ago
The exact characters that are escaped by this function are the null byte (0), newline (\n), carriage return (\r), backslash (\), single quote ('), double quote (") and substiture (SUB, or \032).
To Top