[3.0.x.x] DB driver connects as utf8 but tables are utf8mb4 since 3.0.5.0: emoji in search causes HTTP 500 (error 1267)
What version of OpenCart are you reporting this for?
3.0.5.1. The current 3.0.x.x branch is also affected.
Describe the bug
Since #15233 (3.0.5.0), install/opencart.sql and upgrade 1010.php use utf8mb4_unicode_ci for all tables. But upload/system/library/db/mysqli.php line 17 still calls set_charset('utf8'), which is 3-byte utf8mb3. A 4-byte character such as an emoji can't be converted to the column's charset, so MariaDB/MySQL rejects the query:
PHP Warning: mysqli::query(): (HY000/1267): Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb3_general_ci,COERCIBLE) for operation 'like' in system/library/db/mysqli.php on line 25This is followed by an uncaught exception from ModelCatalogProduct::getTotalProducts() (called from ControllerProductSearch::index()), and the visitor gets an HTTP 500 with a blank page. It affects storefront product search, which spam bots routinely hit with emoji strings. The utf8 connection also means utf8mb4 tables can't store 4-byte characters properly, which defeats the purpose of #15233.
To Reproduce
- Run OpenCart 3.0.5.1 (tables are
utf8mb4_unicode_ci). - Open
index.php?route=product/search&search=%F0%9F%91%89(). The same happens with&tag=%F0%9F%91%89. - You get HTTP 500, and error 1267 is logged.
Expected behavior The normal "no products match" search page.
Fix
In upload/system/library/db/mysqli.php line 17, change set_charset('utf8') to set_charset('utf8mb4'), the same as master.
upload/system/library/db/pdo.php lines 14–16 have the same mismatch (SET NAMES 'utf8', SET CHARACTER SET utf8, SET CHARACTER_SET_CONNECTION=utf8).
Tested on a production store with the mysqli driver. Emoji searches went from 500 to 200. Home, product, category, search, sitemap and admin login pages were byte-identical before and after. Normal search results were unchanged. After the fix, an emoji search term is also stored correctly in oc_customer_search (bytes F09F9189).
Server / Test environment
- Deployed to a web server (production)
- Linux, LiteSpeed
- PHP 8.2.33
- MariaDB 11.4.13
- Upgraded from 3.0.3.9 to 3.0.5.1. A fresh 3.0.5.1 install creates the same
utf8mb4_unicode_citables with the same driver, so it should reproduce there too, though I only tested the upgraded install.
Source: opencart/opencart