Documentation Home
MySQL 8.0 C API Developer Guide
Download this Manual
PDF (US Ltr) - 1.3Mb
PDF (A4) - 1.3Mb


5.4.61 mysql_real_escape_string_quote()

unsigned longmysql_real_escape_string_quote(MYSQL *mysql,                               char *to,                               const char *from,                               unsigned long length,                               char quote)

Description

This function creates a legal SQL string for use in an SQL statement. SeeString Literals.

Themysql argument must be a valid, open connection because character escaping depends on the character set in use by the server.

The string in thefrom argument is encoded to produce an escaped SQL string, taking into account the current character set of the connection. The result is placed in theto argument, followed by a terminating null byte.

Characters encoded are\,',",NUL (ASCII 0),\n,\r, Control+Z, and`. Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped.mysql_real_escape_string_quote() quotes the other characters to make them easier to read in log files. For comparison, see the quoting rules for literal strings and theQUOTE() SQL function inString Literals, andString Functions and Operators.

Note

If theANSI_QUOTES SQL mode is enabled,mysql_real_escape_string_quote() cannot be used to escape double quote characters for use within double-quoted identifiers. (The function cannot tell whether the mode is enabled to determine the proper escaping character.)

The string pointed to byfrom must belength bytes long. You must allocate theto buffer to be at leastlength*2+1 bytes long. (In the worst case, each character may need to be encoded as using two bytes, and there must be room for the terminating null byte.) Whenmysql_real_escape_string_quote() returns, the contents ofto is a null-terminated string. The return value is the length of the encoded string, not including the terminating null byte.

Thequote argument indicates the context in which the escaped string is to be placed. Suppose that you intend to escape thefrom argument and insert the escaped string (designated here bystr) into one of the following statements:

1) SELECT * FROM table WHERE name = 'str'2) SELECT * FROM table WHERE name = "str"3) SELECT * FROM `str` WHERE id = 103

To perform escaping properly for each statement, callmysql_real_escape_string_quote() as follows, where the final argument indicates the quoting context:

1) len = mysql_real_escape_string_quote(&mysql,to,from,from_len,'\'');2) len = mysql_real_escape_string_quote(&mysql,to,from,from_len,'"');3) len = mysql_real_escape_string_quote(&mysql,to,from,from_len,'`');

If you must change the character set of the connection, use themysql_set_character_set() function rather than executing aSET NAMES (orSET CHARACTER SET) statement.mysql_set_character_set() works likeSET NAMES but also affects the character set used bymysql_real_escape_string_quote(), whichSET NAMES does not.

Example

The following example inserts two escaped strings into anINSERT statement, each within single quote characters:

char query[1000],*end;end = my_stpcpy(query,"INSERT INTO test_table VALUES('");end += mysql_real_escape_string_quote(&mysql,end,"What is this",12,'\'');end = my_stpcpy(end,"','");end += mysql_real_escape_string_quote(&mysql,end,"binary data: \0\r\n",16,'\'');end = my_stpcpy(end,"')");if (mysql_real_query(&mysql,query,(unsigned int) (end - query))){   fprintf(stderr, "Failed to insert row, Error: %s\n",           mysql_error(&mysql));}

Themy_stpcpy() function used in the example is included in thelibmysqlclient library and works likestrcpy() but returns a pointer to the terminating null of the first parameter.

Return Values

The length of the encoded string that is placed into theto argument, not including the terminating null byte.

Errors

None.