|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
|
Thank you for your help!
If the status of the bug report you submitted changes, you will be notified.
You may return here and check the status or update your report at any time. The URL for your bug report is: https://bugs.php.net/bug.php?id=44998.
[2008-05-14 20:48 UTC] djneoform at gmail dot com
Description:
------------
When dealing with binary strings that are being escaped and inserted into a mysql table that contains a BINARY(128) field, the data that ends up being inserted is incomplete if the string contains a null character within the string.
Reproduce code:
---------------
/*
--TABLE DEF:
CREATE TABLE `system_users_stored_sessions` (
`session` binary(128) NOT NULL,
`name` char(32) NOT NULL,
`posted_on` datetime NOT NULL,
`body` varchar(65000) NOT NULL,
PRIMARY KEY (`session`,`name`),
KEY `posted_on` (`posted_on`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/
$hash_with_null_char = hash('whirlpool', '1210797760.8178', true);
$mysqli->query("
INSERT INTO
user_sessions
SET
session = '".$mysqli->real_escape_string($hash_with_null_char)."',
posted_on = NOW(),
name = '".$mysqli->real_escape_string($name)."',
body = '".$mysqli->real_escape_string($body)."'
");
Expected result:
----------------
Expecting a row containing the full binary hash.
Actual result:
--------------
Actual result: a binary string that is terminated by the null char in the string, not the actual ending.
I am able to work around this by doing a addslashes() to the string before doing the mysqli_real_escape_string(), however all other chars are double escaped making the resulting string be longer than it wanted (128 binary chars).
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Feb 07 03:00:01 2026 UTC |
What MySQL version are you using? Interesting find. The mysqli_real_escape_string test does play with \0: if ("foo\\0bar" !== ($tmp = mysqli_real_escape_string($link, "foo" . chr(0) . "bar"))) printf("[009] Expecting %s, got %s\n", "foo\\0bar", $tmp); But it does not try to insert/fetch such a string. Of course it works fine with PHP 5.3 and a recent MySQL and on Linux...I was using v.5.0.51b 64bit (win2k3) at the time. Right now I'm using a compiled version of the enterprise code, 5.0.62 from apachelounge.com. When I do an insert a value: mysqli_real_escape_string('foo'.chr(0x0).'bar') all i see in the table after is "foo" maybe this is a windows only issue?The resulting dump for the table once i run the script. /* MySQL Data Transfer Source Host: localhost Source Database: phpneoform Target Host: localhost Target Database: phpneoform Date: 7/24/2008 10:22:31 AM */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for test_table -- ---------------------------- CREATE TABLE `test_table` ( `id` int(10) unsigned NOT NULL auto_increment, `name` binary(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `test_table` VALUES ('1', 'AAAAAAAAAAAAAAAAAAAAAAAAA');But this can't be expected behavior, my string is being truncated improperly. If I manually run the mysql query in the cli, I get proper results: mysql> INSERT INTO `test_table` VALUES ('2', 'AAAAAAAAAAAAAAAAAAAAAAAAA\0BBBBBBBBBBBBBBBBBBB'); mysql> select * from test_table; +----+------------------------------------------------------------------------------------------------------+ | id | name | +----+------------------------------------------------------------------------------------------------------+ | 1 | AAAAAAAAAAAAAAAAAAAAAAAAA | | 2 | AAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBB | +----+------------------------------------------------------------------------------------------------------+ If I do the exact same insert query in PHP, I lose half my string. Clearly this is not due to mysql.