nathansizemore
nathansizemore

Reputation: 3196

Weird MongoDB Error PHP

I have been using MongoDB and PHP for a few months now. I am doing some testing on a couple new features, out of nowhere MongoDB is acting weird. It is truncating the first character in a string when I insert it into the Database. My code has not changed, only the results? I have a text form called details. When I submit and insert it into the collection, the details entries gets all screwy. If I enter "Tomato" into the form, I will receive:

From PHP echo (building from a query for the entry after insertion):

"omato"

From the Mongo Shell I receive:

"Details" : ""

From an api request (echos JSON) I receive:

"Details" : "\u0000omato"

Any thoughts on what is causing this?

Upvotes: 1

Views: 86

Answers (1)

Derick
Derick

Reputation: 36784

It seems like you have for some reason added a \0 character to a string. The three different tools handle this all in different ways:

  1. PHP's echo simply displays it, and hence it doesn't appear. If you'd stream the output through something like hexdump you'll see the \0 character.

  2. The mongo shell truncates the display when it encounters the \0 character, and hence you see nothing.

  3. PHP's JSON encoder encodes a \0 character as "\u0000".

Upvotes: 1

Related Questions