note 111870 added to mysqli.quickstart.statements
| From: | developer at 4seasonsconsulting dot com | Date: | Sat, 06 Apr 2013 04:58:08 +0000 |
| Subject: | note 111870 added to mysqli.quickstart.statements | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194362@lists.php.net to get a copy of this message | ||
When accessing columns of type ENUM or SET, your results may not always come back as expected.
Given:
SQL:
CREATE TABLE
items (
item_key char(4) NOT NULL DEFAULT 'xxxx',
item_id smallint(5) unsigned NOT NULL,
item_media
enum('pdf','tape','cd','dvd','mp3','mp4')
NOT NULL DEFAULT 'mp3',
item_file varchar(40) DEFAULT NULL,
PRIMARY KEY (item_key,item_id,item_media)
) ENGINE=MyISAM;
INSERT INTO items (item_key, item_id,
item_media) VALUES
('A1B2', 1, 'mp3'),
('A1B2', 2, 'mp4'),
('A1B2', 3, 'pdf'),
('A1B2', 4, 'dvd'),
('A1B2', 5, 'mp3');
<?php
...
$result = mysql_query("SELECT item_id, item_media+0 FROM
items
WHERE item_key='A1B2' ORDER BY item_media";
while ($row = mysql_fetch_array($result) )
echo 'id=' . $row[0] . ', media=' . $row[1] . "\r\n";
...
?>
When just the query is run in phpMyAdmin,
you most likely will get:
id=3, media=1
id=4, media=4
id=1, media=5
id=5, media=5
id=2, media=6
but php will give you:
id=1, media=0
id=2, media=0
id=3, media=0
id=4, media=0
id=5, media=0
This is because the translation of <enum or set>+0
is evaluated as a string (converted to zero)+0,
This does need further research for a fix.
----
Server IP: 69.147.83.199
Probable Submitter: 75.37.223.93
----
Manual Page -- http://php.net/manual/en/mysqli.quickstart.statements.php
Edit -- https://master.php.net/note/edit/111870
Del: integrated -- https://master.php.net/note/delete/111870/integrated
Del: useless -- https://master.php.net/note/delete/111870/useless
Del: bad code -- https://master.php.net/note/delete/111870/bad+code
Del: spam -- https://master.php.net/note/delete/111870/spam
Del: non-english -- https://master.php.net/note/delete/111870/non-english
Del: in docs -- https://master.php.net/note/delete/111870/in+docs
Del: other reasons-- https://master.php.net/note/delete/111870
Reject -- https://master.php.net/note/reject/111870
Search -- https://master.php.net/manage/user-notes.php