mysqli_fetch_assoc replaces newline characters [closed]

1 day ago 1
ARTICLE AD BOX

I have a strange issue with mysqli_fetch_assoc().

On the dev server, everything works correctly.
On my local environment, mysqli_fetch_assoc() replaces \n with \r\n in text fields returned from the database.

Because of this, unserialize() breaks with:

unserialize(): Error at offset X of Y bytes

The serialized string is stored in the database, and the extra \r characters change the string length, so unserialize fails.

Important details:

Dev and local environments both use:

PHP 8.2.26

MariaDB 11.2

Locally I use ubuntu 22.04 and docker.

Database structure is identical (SHOW CREATE TABLE matches)

Charset is utf8mb3 on both servers

PHP versions are identical

phpinfo output seems to be the same (at least everything connected with mysql, collation and encoding)

Connection settings are identical

mysqlnd is used in both environments and version is the same

What I have already tried:

Verified PHP version matches exactly

Verified MariaDB version matches exactly

Verified table structure and charset

Checked sql_mode

Tried:

SET sql_mode='NO_BACKSLASH_ESCAPES'; SET character_set_results=binary;

Compared phpinfo() outputs

Verified client/server charset variables

Confirmed the raw value in DB contains only \n, not \r\n

I want to solve this at a low level (MySQL / mysqli / PHP driver level).
Replacing characters in PHP is not an acceptable solution.

The question is:

What can cause mysqli_fetch_assoc to transform \n into \r\n on one environment but not another, if PHP and MariaDB versions are identical?

I have already searched Google and asked AI for possible causes but have not found a solution.

Any ideas what else could cause this behavior?

Read Entire Article