Strange behavior in a class function

Function exits after while loop

Page 1 of 1

2 Replies - 565 Views - Last Post: 22 September 2008 - 07:25 AM Rate Topic: -----

#1 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2503
  • View blog
  • Posts: 8,564
  • Joined: 08-August 08

Strange behavior in a class function

Posted 22 September 2008 - 05:12 AM

As it's written, this code only displays the var_dump($row) and $row[1]. If I comment out the first while loop, it will display var_dump($row2), but not echo "<br>end..."
Any ideas?

	function ReadContent($Cid)
		{
			$query = "SELECT * FROM $this->mytable where $this->myindex = $Cid";
			$dbdata = mysql_query($query) or die(mysql_error());
 			while ($row = mysql_fetch_array($dbdata) or die(mysql_error()))
 			{
			var_dump($row);
			echo $row[1];
			}
			echo $row[2];
			$query = "SELECT * FROM $this->myfields where $this->myfieldindex = $Cid";
			echo "<br>query: <br>";
			$ndbdata= mysql_query($query) or die(mysql_error());
			while ($row2 = mysql_fetch_array($ndbdata) or die(mysql_error()))
			{
			echo "<br>row2: <br>";
			var_dump($row2);
			}
			echo "<br>end...";
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Strange behavior in a class function

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 438
  • View blog
  • Posts: 792
  • Joined: 17-June 08

Re: Strange behavior in a class function

Posted 22 September 2008 - 07:05 AM

You need to lose the or die(mysql_error()) in your while() loop condition. The mysql_fetch_array() function returns false when there are no more records in the result set, and when it returns false, the second half of that or expression in the while loop is going to execute - the die(). So, as you've written it, when the query runs out of records, instead of just the while loop exiting, it exits the entire program.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2503
  • View blog
  • Posts: 8,564
  • Joined: 08-August 08

Re: Strange behavior in a class function

Posted 22 September 2008 - 07:25 AM

That did it. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1