Page 1 of 1

Char(1) - Create all possible values..

Posted: Mon Jul 17, 2006 12:56 am
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?

Posted: Mon Jul 17, 2006 6:48 am
by feyd

Code: Select all

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

Posted: Mon Jul 17, 2006 6:55 am
by Benjamin
Awesome, thank you very much feyd.

Posted: Tue Jul 18, 2006 12:12 am
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.

Posted: Tue Jul 18, 2006 12:45 am
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..