Add mysql_set_charset() so that the connection encoding can be changed. This is similar to the SET NAMES statement but allows the mysql_real_escape_string to use the correct character set.

This commit is contained in:
Scott MacVicar 2007-05-14 17:10:47 +00:00
parent a640896685
commit ac38b635cb
3 changed files with 42 additions and 1 deletions

2
NEWS
View File

@ -1,6 +1,8 @@
PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? 2007, PHP 5.2.3
- Added function mysql_set_charset(). Allows connection encoding to be altered
at run time. (Scott)
- Allow SOAP extension's handler() to work even when
always_populate_raw_post_data is off. (Ilia)
- Fixed ext/filter Email Validation Vulnerability (MOPB-24 by Stefan Esser)

View File

@ -101,6 +101,10 @@ static int le_result, le_link, le_plink;
#define MYSQL_HAS_YEAR
#endif
#if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007
#define MYSQL_HAS_SET_CHARSET
#endif
#define MYSQL_ASSOC 1<<0
#define MYSQL_NUM 1<<1
#define MYSQL_BOTH (MYSQL_ASSOC|MYSQL_NUM)
@ -181,7 +185,9 @@ zend_function_entry mysql_functions[] = {
#endif
PHP_FE(mysql_info, NULL)
#ifdef MYSQL_HAS_SET_CHARSET
PHP_FE(mysql_set_charset, NULL)
#endif
/* for downwards compatability */
PHP_FALIAS(mysql, mysql_db_query, NULL)
PHP_FALIAS(mysql_fieldname, mysql_field_name, NULL)
@ -1124,6 +1130,36 @@ PHP_FUNCTION(mysql_client_encoding)
/* }}} */
#endif
#ifdef MYSQL_HAS_SET_CHARSET
/* {{{ proto bool mysql_set_charset(string csname [, int link_identifier])
sets client character set */
PHP_FUNCTION(mysql_set_charset)
{
zval *mysql_link = NULL;
char *csname;
int id = -1, csname_len;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &csname, &csname_len, &mysql_link) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (ZEND_NUM_ARGS() == 1) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (!mysql_set_character_set(&mysql->conn, csname)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
#endif
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
/* {{{ proto bool mysql_create_db(string database_name [, int link_identifier])

View File

@ -91,6 +91,9 @@ PHP_FUNCTION(mysql_stat);
PHP_FUNCTION(mysql_thread_id);
PHP_FUNCTION(mysql_client_encoding);
PHP_FUNCTION(mysql_ping);
#if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007
PHP_FUNCTION(mysql_set_charset);
#endif
ZEND_BEGIN_MODULE_GLOBALS(mysql)
long default_link;