php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48453 fetch_assoc() problem
Submitted: 2009-06-02 15:13 UTC Modified: 2011-11-04 10:07 UTC
Votes:16
Avg. Score:4.9 ± 0.5
Reproduced:16 of 16 (100.0%)
Same Version:8 (50.0%)
Same OS:6 (37.5%)
From: gubbov53 at hotmail dot com Assigned: mysql (profile)
Status: No Feedback Package: MySQLi related
PHP Version: 5.2.9 OS: Windows Vista
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
38 + 33 = ?
Subscribe to this entry?

 
 [2009-06-02 15:13 UTC] gubbov53 at hotmail dot com
Description:
------------
fetch_assoc() (see below) does not work. Hangs Apache.
If "$row=$result->fetch_assoc();" is replaced by "$row=$result->fetch_row();" the code is working... 

Reproduce code:
---------------
$result=$db->query($query);
$num_results=$result->num_rows;
echo "<p>Number of books found: ".$num_results."</p>";
for ($i=0; $i<$num_results; $i++) {
$row=$result->fetch_assoc(); // Does not work!!!
...


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-03 12:48 UTC] gubbov53 at hotmail dot com
To have this problem set up a small MySQL db by running the following as root:
------------
create database books_db;
use books_db;
create table books
(  isbn char(13) not null primary key,
   author char(50),
   title char(100),
   price float(4,2)
);
grant select, insert, update, delete
on books_db.*
to books_user@localhost identified by 'password';
insert into books values
  ("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99),
  ("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99);
---------------
Then run this file in IE:
--------------

<html>
<body>
<?php
  @ $db=new mysqli('localhost','books_user','password','books_db');
  if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
  }
  $query="select title,author from books where author like '%Morgan%'";
  $result=$db->query($query);
  $num_results=$result->num_rows;
  echo "<p>Number of books found: ".$num_results."</p>";
  for ($i=0; $i<$num_results; $i++) {
    $row=$result->fetch_assoc(); // loops here...
    echo "<p><strong>".($i+1)." Title: ";
    echo htmlspecialchars(stripslashes($row['title']));
    echo "</strong><br />Author: ";
    echo stripslashes($row['author']);
  }
  $result->free();
  $db->close();
?>
</body>
</html>
 [2009-06-03 20:39 UTC] jani@php.net
Please read this: http://bugs.php.net/how-to-report.php
 [2009-06-04 08:41 UTC] gubbov53 at hotmail dot com
Tried with the latest PHP Version 5.2.10RC2-dev but the problem remains.
 [2009-06-04 08:44 UTC] pajoye@php.net
There is a script to test.
 [2009-06-06 08:06 UTC] jacobus0223 at hotmail dot com
I have this problem too and also have the same problem with fetch_object().  The browser hangs and finally times out.  

I also note that there was a problem with these same two functions back in 2002 -- http://bugs.php.net/bug.php?id=18622.
 [2009-06-09 19:31 UTC] gubbov53 at hotmail dot com
A temp solution to have it working if you have code with fetch_assoc() is to replace fetch_assoc() with fetch_fields() (to get keys), fetch_row() (to get values), and array_combine(). See example below...

<html>
<body>
<?php
  @ $db=new mysqli('localhost','books_user','password','books_db');
  if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
  }
  $query="select title,author from books where author like '%Morgan%'";
  $result=$db->query($query);
  $num_results=$result->num_rows;
  echo "<p>Number of books found: ".$num_results."</p>";

  $finfo = $result->fetch_fields(); $nc=0; foreach ($finfo as $val) {$row_key[$nc++]=$val->name;} //---
  for ($i=0; $i<$num_results; $i++) {
    //$row=$result->fetch_assoc();
    $row_val=$result->fetch_row();$row=array_combine($row_key,$row_val); //---
    echo "<p><strong>".($i+1)." Title: ";
    echo htmlspecialchars(stripslashes($row['title']));
    echo "</strong><br />Author: ";
    echo stripslashes($row['author']);
  }
  $result->free();
  $db->close();
?>
</body>
</html>
 [2009-08-12 14:09 UTC] mail at maiknowak dot de
php-code:
$sql="SELECT * FROM foo" //causes Windows crash dialog or Apache crash
$sql="SELECT bar, baz FROM foo" //works just fine

$result = $myMysql->query ( $sql );
$row = $result->fetch_assoc();

ver 5.2.10 & 5.3 shipped with Zend Studio 7
 [2010-02-01 22:06 UTC] marco at marcoentertainment dot com
okay i wrote below with no sucess used the code from gubbov and it works as it should :? when i used my code it seems like it didnt parse the for loop properly  but code after it was executed fine so ehhh but thanks gubbov

<?php 

$link = mysqli_connect(localhost,*******,'******') or die;

mysqli_select_db($link, o******) or die;

if (mysqli_connect_errno()) {
	echo 'Error: CNC';
	exit;
}

 $query = "SELECT a.product_price, b.product_name, b.product_thumb_image, c.category_id, a.product_id

	FROM jos_vm_product_price AS a, jos_vm_product AS b, jos_vm_product_category_xref AS c WHERE a.product_price >= 100 AND a.product_id = b.product_id AND c.product_id = a.product_id AND b.product_id = c.product_id AND
    
    b.product_publish = 'Y' ORDER BY a.product_price";
	
	$result = mysqli_query($link, $query);
	
	$num_results = mysqli_num_rows($result); 
	
	echo "<p>Number Found: ".$num_results."</p>";

	for ($i=0, $i <$num_results; $i++) {
		
		$row = mysqli_fetch_row($result);
		
		echo "<p>".($i+1).". Price: ";
		
		echo ($row['product_price']);
		
		echo "</p>";
		
		}
	
	mysqli_free_result($result);
	
	mysqli_close($link);

?>
 [2010-02-01 22:11 UTC] marco at marcoentertainment dot com
oh yeah i also got a syntax error in my for loop at **** , $i++) which went away when a semicolon was added ie. $i++;) 

i'm pretty tired so i prob missed something but still was odd
 [2010-08-13 11:54 UTC] andrey@php.net
-Status: Assigned +Status: Feedback
 [2010-08-13 11:54 UTC] andrey@php.net
Can you reproduce this with 5.3?
 [2011-11-04 10:07 UTC] uw@php.net
-Status: Feedback +Status: No Feedback
 [2011-11-04 10:07 UTC] uw@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 00:01:28 2024 UTC