Jump to content

Mysql num_rows shows 3 results, fetch array outputs only 2.


Ivan Ivković

Recommended Posts

This is the method from the picture model.


public function fetchAlbumPics($user_id, $city_id, $cat_id){
	if($cat_id == 0 && $user_id != 0 && $city_id != 0)
		$query = 'SELECT * FROM sc_pics WHERE user_id="' . $user_id . '" AND city_id="' . $city_id . '"';
	if($cat_id != 0 && $city_id != 0 && $user_id != 0)
		$query = 'SELECT * FROM sc_pics WHERE user_id="' . $user_id . '" AND city_id="' . $city_id . '" AND cat_id="' . $cat_id . '"';

	$result = $this -> db -> query($query);
	$return = $result -> num_rows ? $result : false;
	return $return;
}

This is where it's being called.


$fetch = $pic -> fetchAlbumPics($user_id, $city_id, $cat_id);
		if($fetch !== false){
			$this -> registry -> template -> pics = $fetch;
		}

 

This is the view where it's being outputted:


<?php

	if(isset($pics)){
                        echo $pics -> num_rows;
		while($fetch = $pics -> fetch_array()){
			$description = $fetch['description'] == '' ? 'No Description' : $fetch['description'];
			?>
			<div class="element item">

				<a class="item_link" href="/photo/view/<?php echo $fetch['pic_id'] ?>">
					<img class="img" src="/thumb.php?pic=content/pics/<?php echo $fetch['user_id'] . $fetch['src'] ?>&h=250&w=250"/>
				</a>

			</div>
			<?php

		}
	}

?>

 

Result: mysql_num_rows returns 3 results, the script outputs only 2 results/pictures.

 

SQL query in PHPMYADMIN outputs 3 results... This loops only 2 results, I don't know why. There's no bug inside the loop, since I've tried only echoing number 0 instead of outputting information. Only outputs 2 times. I don't know how to fix this for now.

 

And the problem is NOT in the query, the query has been tested.

Link to comment
Share on other sites

After a more thorough look through your code, I still don't see anything that could explain this problem.

 

You do have a problem with a BOM and the wrong charset being sent in the HTTP header, but nothing of this should have any significance on the loop. The only thing I can think of, is that fetch_array () must (somehow) be returning something that evaluates to false.

Either put 3x var_dump ($result->fetch_array()) after retrieving the data from the database, or directly in front of the loop, or use a debugger to run through a step-by-step process until you find the actual problem.

Link to comment
Share on other sites

Unfortunately that doesn't help me at all, but it did highlight another possibility: There's a rogue fetch_array () (or similar) in your script somewhere.

This only underlines the need of a proper debugger, so that you can step through the code line by line. Only then will you be able to accurately, and quickly, find where the error occurs. Nothing further I, nor anyone else, can do without full access to your development system.

Link to comment
Share on other sites

As Christian mentioned use var_dump function.

Put in this piece of code immediately after while loop and post out the result.

Put curly brackets in your if statements, too ....

echo '<pre>'.var_dump($fetch).'</pre>'; exit;

 

Link to comment
Share on other sites

Sorry, I meant inside in the beginning in the while loop .

 

while($fetch = $pics -> fetch_array()){
echo '<pre>'.var_dump($fetch).'</pre>'; exit;
			$description = $fetch['description'] == '' ? 'No Description' : $fetch['description'];
			?>
			<div class="element item"> etc......

Link to comment
Share on other sites

jesirose: From what I can see, and assume, it is a MySQLi object. Though, fair question as you never know...

As for it not making any sense, thus my comment about stepping through the code with a debugger. I'm almost positive there's some hidden gotcha in the code somewhere.

Link to comment
Share on other sites

Hey guys I solved the error. However, I still don't understand it. May be a bug in the MySQLi extension itself..

 

Instead of:

 

$fetch = $pic -> fetchAlbumPics($user_id, $city_id, $cat_id);
if($fetch !== false){
$this -> registry -> template -> result = $fetch;
}

 

I've put.

 

$fetch = $pic -> fetchAlbumPics($user_id, $city_id, $cat_id);
if($fetch !== false){
$this -> registry -> template -> result = $pic -> fetchAlbumPics($user_id, $city_id, $cat_id);
}

 

Still, don't know why this was the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.