note 52506 added to function.mysql-real-escape-string

From: Date: Tue, 03 May 2005 23:05:45 +0000
Subject: note 52506 added to function.mysql-real-escape-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-88916@lists.php.net to get a copy of this message
mysql_real_escape_string() does not work for BINARY values!!! I'm using PHP 4.3.10 and found my web application was storing incorrect values in the database after using mysql_real_escape_string(pack()). Anyone trying to put raw data into a SQL statement should use hex values. Once you have a hex string, use my convenient function for making mySQL happy: function db_hex_to_bin($hexval) { return("CAST(0x$hexval AS BINARY)"); } Example: $binvalue = 'f9c3'; $sqlvalue = db_hex_to_bin($binvalue); $query = "SELECT * FROM mytable WHERE binfield = $sqlvalue"; Both mySQL and PHP have extremely poor documentation for binary functionality, and there may be some secret tricks I don't know about. On the other hand, hex values make debugging MUCH easier. Enjoy, -- Miqro ---- Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52506 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52506&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52506&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52506&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52506&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52506&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52506&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88916) next »