mysqli::real_escape_string

(PHP 5, PHP 7, PHP 8)

mysqli::real_escape_string -- mysqli_real_escape_string — 연결의 현재 문자 집합을 고려하여 SQL 문에서 사용하기 위해 문자열의 특수 문자를 이스케이프합니다.


설명

객체 지향 스타일

public mysqli::real_escape_string(string $string): string

절차적 스타일

mysqli_real_escape_string(mysqli $mysql, string $string): string

이 함수는 SQL 문에서 사용할 수 있는 유효한 SQL 문자열을 만드는 데 사용됩니다. 주어진 문자열은 연결의 현재 문자 집합을 고려하여 이스케이프된 SQL 문자열을 생성하도록 인코딩됩니다.

주의

보안: 기본 문자 집합

문자 집합은 서버 수준에서 설정하거나 mysqli_real_escape_string()에 영향을 미치도록 API 함수 mysqli_set_charset()로 설정해야 합니다. 자세한 내용은 character sets에 대한 개념 섹션을 참조하세요.


매개변수

mysql
절차적 스타일 전용: mysqli_connect() 또는 mysqli_init()에 의해 반환된 mysqli 객체
string
이스케이프할 문자열입니다.

인코딩된 문자는 NUL (ASCII 0), \n, \r, \, ', ", 및 Control-Z입니다.


반환 값

이스케이프된 문자열을 반환합니다.


Examples

예제 #1 mysqli::real_escape_string() 예제

객체 지향 스타일

                  
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$city = "'s-Hertogenbosch";

/* this query with escaped $city will work */
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'",
    $mysqli->real_escape_string($city));
$result = $mysqli->query($query);
printf("Select returned %d rows.\n", $result->num_rows);

/* this query will fail, because we didn't escape $city */
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'", $city);
$result = $mysqli->query($query);
                  
                

절차적 스타일

                  
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");

$city = "'s-Hertogenbosch";

/* this query with escaped $city will work */
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'",
    mysqli_real_escape_string($mysqli, $city));
$result = mysqli_query($mysqli, $query);
printf("Select returned %d rows.\n", mysqli_num_rows($result));

/* this query will fail, because we didn't escape $city */
$query = sprintf("SELECT CountryCode FROM City WHERE name='%s'", $city);
$result = mysqli_query($mysqli, $query);
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

Select returned 1 rows.

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's-Hertogenbosch'' at line 1 in...
                

기타