diff --git a/core/database_api.php b/core/database_api.php index fd2af58ed3..efaef42957 100644 --- a/core/database_api.php +++ b/core/database_api.php @@ -322,7 +322,12 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = $lastoffset = 0; $i = 1; if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) { - while( preg_match( '/(\?)/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) { + while( preg_match( '/\?/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) { + $matches = $matches[0]; + # Realign the offset returned by preg_match as it is byte-based, + # which causes issues with UTF-8 characters in the query string + # (e.g. from custom fields names) + $matches[1] = utf8_strlen( substr( $p_query, 0, $matches[1]), mb_internal_encoding() ); if( $i <= count( $arr_parms ) ) { if( is_null( $arr_parms[$i - 1] ) ) { $replace = 'NULL'; @@ -346,10 +351,10 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = echo( "Invalid argument type passed to query_bound(): $i" ); exit( 1 ); } - $p_query = utf8_substr( $p_query, 0, $matches[1][1] ) . $replace . utf8_substr( $p_query, $matches[1][1] + utf8_strlen( $matches[1][0] ) ); - $lastoffset = $matches[1][1] + utf8_strlen( $replace ); + $p_query = utf8_substr( $p_query, 0, $matches[1] ) . $replace . utf8_substr( $p_query, $matches[1] + utf8_strlen( $matches[0] ) ); + $lastoffset = $matches[1] + utf8_strlen( $replace ); } else { - $lastoffset = $matches[1][1] + 1; + $lastoffset = $matches[1] + 1; } $i++; }