Char(1) - Create all possible values..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Char(1) - Create all possible values..

Post by Benjamin »

What would be the best way to create an array of all the possible values that a char(1) MySQL database field can hold? I figured I could use range() plus numbers and symbols but I want to maximize the number of possible values I can use.

Based on the ascii chart

http://www.lookuptables.com/

I guess I can use the characters 32 through 126? Or can I use all of them?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$num = range(0, 255);
$chars = array_map('chr', $num);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Awesome, thank you very much feyd.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I ran some tests to insert all of these into a database and then pull them back out to make sure they match. The only problem I had is with character 32, which is a space. For some reason when I pull it back out of the database it's null?

Here is the code I used..

Code: Select all

$num = range(0, 255);
$chars = array_map('chr', $num);

foreach ($chars as $key => $value) {
    $Database->sendQuery("INSERT INTO `test` (`number`, `character`) VALUES ('" . mysql_real_escape_string($key) . "', '" . mysql_real_escape_string($value) . "')");
}

$Database->sendQuery("SELECT * FROM `test` ORDER BY `number` asc");

while ($Data = $Database->fetchRow()) {
    if ($Data['character'] != $chars[$Data['number']]) {
        echo 'WARNING!<br />';
        echo 'CHARACTER NUMBER ' . $Data['number'] . " does not match. |" . $chars[$Data['number']] . '|' . $Data['character'] . '|<br />';
    } else {
        echo 'Matched!<br />';
    }
}
WARNING!
CHARACTER NUMBER 32 does not match. | ||
EDIT: I tried not escaping the space but it still isn't working.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

:cry: :cry:

Looks like it's a bug in MySQL.

http://bugs.mysql.com/bug.php?id=19564

This is for version 5.0.22 and I am running 4.1.16 so maybe it's been around for a while?!?!

Guess I'll just unset character 32..
Post Reply