I've run into a strange problem that I've never encountered before with prepared statements. I'm using the built in PHP MySQLi functions to insert data into the database.
The code looks like this:
$mysqli = new mysqli("localhost", "user", "pass", "test");
$mysqli->set_charset('utf8');
$stmt = $mysqli->prepare("INSERT INTO `lines` (`description`) VALUES (?)");
$stmt->bind_param("s", $_POST['description']);
$stmt->execute();
In a textarea named 'description', the following string is entered:
Alice likes Bob’s cat.
Note that the apostrophe is not the regular ' kind, but one that I got from copying the line out of an email (in OS X's Mail app).
When the textarea is submitted as part of a form, the value that gets stored in the field in the table is:
Alice likes Bob
The rest is cut off. I've debugged as much as I can think of, and I know the following to be true:
- The value of the $_POST['description'] variable is correct before it is placed in the bind_param function.
- The database tables should be using UTF8 (I have tried collations of utf8_bin and utf8_unicode_ci but neither seem to work).
- When I use something like phpMyAdmin to enter the same string, it works.
I'd originally thought it was an encoding problem, but point (3) seems to contradict that, so the only thing I'm left with is something wrong with the way I've set up the statement.
If anyone could shed some light it would be most helpful.