#29335 [Opn->Csd]: mysqli_fetch_array resulttype
| From: | georg@php.net | Date: | Mon, 26 Jul 2004 07:22:46 +0000 |
| Subject: | #29335 [Opn->Csd]: mysqli_fetch_array resulttype | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-63537@lists.php.net to get a copy of this message | ||
ID: 29335
Updated by: georg@php.net
Reported By: mjs15451 at hotmail dot com
-Status: Open
+Status: Closed
Bug Type: MySQL related
Operating System: Linux
PHP Version: 5.0.0
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2004-07-26 08:48:56] mjs15451 at hotmail dot com
CREATE TABLE
mailbox (
username varchar(255) NOT NULL default '',
password varchar(255) NOT NULL default '',
name varchar(255) NOT NULL default '',
maildir varchar(255) NOT NULL default '',
quota int(10) NOT NULL default '-1',
domain varchar(255) NOT NULL default '',
created datetime NOT NULL default '0000-00-00 00:00:00',
modified datetime NOT NULL default '0000-00-00 00:00:00',
active tinyint(4) NOT NULL default '1',
PRIMARY KEY (username),
KEY username (username)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO mailbox VALUES ('user1', 'testtest', 'User
1',
'example.com/user1/', 0, 'example.com', '2004-07-25 00:00:00',
'2004-07-25 00:00:00', 1);
INSERT INTO mailbox VALUES ('user2', 'testtest', 'User
2',
'example.com/user2/', 0, 'example.com', '2004-07-25 00:00:00',
'2004-07-25 00:00:00', 1);
<?php
$link = mysqli_connect ('localhost','postfix','postfix');
$success = mysqli_select_db ($link, 'postfix');
$result = mysqli_query ($link, 'SELECT * FROM mailbox');
//Good Code which prints username
while ($row = mysqli_fetch_array ($result, MYSQLI_BOTH)){
print "username:" . $row['username'] . "<br>";
}
$result = mysqli_query ($link, 'SELECT * FROM mailbox');
//Bad Code which doesn't print username
while ($row = mysqli_fetch_array ($result)){ //notice MYSQLI_BOTH,
MYSQL_ASSOC or MYSQLI_NUM missing
print "username:" . $row['username'] . "<br>"; //username will
not
output
}
?>
According to the docs on
http://us2.php.net/manual/en/function.mysqli-fetch-array.php:
mixed mysqli_fetch_array ( object result [, int resulttype])
The optional second argument resulttype is a constant indicating what
type of array should be produced from the current row data. The
possible values for this parameter are the constants MYSQLI_ASSOC,
MYSQLI_NUM, or MYSQLI_BOTH. By default the mysqli_fetch_array()
function will assume MYSQLI_BOTH for this parameter.
I don't see this happening with the second loop of this query. I am
running Linux kernel 2.4.22, Apache 2.0.50, PHP 5.0.0 and Mysql
4.1.3beta
------------------------------------------------------------------------
[2004-07-23 18:07:37] georg@php.net
I can't reproduce it.
please provide a short reproducable sample script.
------------------------------------------------------------------------
[2004-07-22 18:25:46] mjs15451 at hotmail dot com
Description:
------------
If a resulttype is not specified when looping through a query, the
resulting array from mysqli_fetch_array does not return any data.
MYSQLI_BOTH should be the default value for mysqli_fetch_array.
I'm also using MySQL 4.1.3beta
Reproduce code:
---------------
while ($row = mysqli_fetch_array($result)){
echo $row[0];
}
Expected result:
----------------
$row[0] should return the first column of the query.
Actual result:
--------------
The while loop executes for the number of rows returned in the query
but $row[0] does not return any data.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=29335&edit=1