php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29335 mysqli_fetch_array resulttype
Submitted: 2004-07-22 18:25 UTC Modified: 2004-07-26 09:22 UTC
From: mjs15451 at hotmail dot com Assigned:
Status: Closed Package: MySQL related
PHP Version: 5.0.0 OS: Linux
Private report: No CVE-ID: None
 [2004-07-22 18:25 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-23 18:07 UTC] georg@php.net
I can't reproduce it. 
 
please provide a short reproducable sample script. 
 [2004-07-26 08:48 UTC] 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-26 09:22 UTC] georg@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC