C API Function Descriptions
2179
Note that
mysql
must be a valid, open connection. This is needed because the escaping depends on
the character set in use by the server.
Description
This function is used to create a legal SQL string that you can use in an SQL statement. See
Section 9.1.1, “String Literals”
.
The string in
from
is encoded to an escaped SQL string, taking into account the current character
set of the connection. The result is placed in
to
and a terminating null byte is appended. Characters
encoded are “
\
”, “
'
”, “
"
”,
NUL
(ASCII 0), “
\n
”, “
\r
”, and Z. 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()
quotes the other characters to make them easier to read in log
files. For comparison, see the quoting rules for literal strings and the
QUOTE()
[892]
SQL function in
Section 9.1.1, “String Literals”
, and
Section 12.5, “String Functions”
.
The string pointed to by
from
must be
length
bytes long. You must allocate the
to
buffer to be at
least
length*2+1
bytes long. (In the worst case, each character may need to be encoded as using
two bytes, and you need room for the terminating null byte.) When
mysql_real_escape_string()
returns, the contents of
to
is a null-terminated string. The return value is the length of the encoded
string, not including the terminating null character.
If you need to change the character set of the connection, use the
mysql_set_character_set()
function rather than executing a
SET NAMES
(or
SET CHARACTER SET
) statement.
mysql_set_character_set()
works like
SET NAMES
but also affects the character set used by
mysql_real_escape_string()
, which
SET NAMES
does not.
Example
char query[1000],*end;
end = strmov(query,"INSERT INTO test_table values(");
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"What is this",12);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"binary data: \0\r\n",16);
*end++ = '\'';
*end++ = ')';
if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
{
fprintf(stderr, "Failed to insert row, Error: %s\n",
mysql_error(&mysql));
}
The
strmov()
function used in the example is included in the
libmysqlclient
library and works
like
strcpy()
but returns a pointer to the terminating null of the first parameter.
Return Values
The length of the value placed into
to
, not including the terminating null character.
Errors
None.
20.6.6.54.
mysql_real_query()
int mysql_real_query(MYSQL *mysql, const char *stmt_str, unsigned long
length)
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...