Ivan Ivković Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 Please post the complete code relevant to the generation and execution of the query. Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 There. This is the live link btw. http://pixpresso.mycyberlove.com/album/list_/26/0/51295 And the problem is NOT in the query, the query has been tested. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 Please look at the page again. Explains it all. The BOM issue you're seeing is just because I'm outputting sample data from controller/model files. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 8, 2012 Share Posted August 8, 2012 Can you reach the thumbnail through the URL in your browser, something like this -> /thumb.php?pic=content/pics/26/13441793754507.jpg Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 Jazzman1: That's not the problem, I verified with the HTML code that there are indeed only 2 blocks generated. Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 Thank you! Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 8, 2012 Share Posted August 8, 2012 Jazzman1: That's not the problem, I verified with the HTML code that there are indeed only 2 blocks generated. Hm...... that's truth, there is no generated html code coming from php/mysql script Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 Ok I can no longer give you sample outputs, I must continue to work on this. If there's anyone who already guesses what's the problem/solution, thank you. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 8, 2012 Share Posted August 8, 2012 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; Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 I have, it displays the result that is not being looped. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 8, 2012 Share Posted August 8, 2012 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...... Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 Jazzman1: That won't help, as it won't be triggered for the missing element. If it was, then there wouldn't be a problem in the first place. Step-by-step debugging is the only (efficient) manner in which this problem can be found and solved. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 8, 2012 Share Posted August 8, 2012 What happens if you replace echo $pics -> num_rows; while($fetch = $pics -> fetch_array()){ With a for loop? for(i=0; $i<$pics -> num_rows; $i++){ $fetch = $pics -> fetch_array(); .... Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 Jazzman1: That won't help, as it won't be triggered for the missing element. If it was, then there wouldn't be a problem in the first place. Step-by-step debugging is the only (efficient) manner in which this problem can be found and solved. Amen. Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 8, 2012 Author Share Posted August 8, 2012 What happens if you replace echo $pics -> num_rows; while($fetch = $pics -> fetch_array()){ With a for loop? for(i=0; $i<$pics -> num_rows; $i++){ $fetch = $pics -> fetch_array(); .... The fetch array is always the first row. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 8, 2012 Share Posted August 8, 2012 What a query you are copied/pasted in phpMyAdmin? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 8, 2012 Share Posted August 8, 2012 That doesn't make sense. Can you post the code for fetch_array()? Is this a function you wrote or are you using a DB class? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 9, 2012 Author Share Posted August 9, 2012 Yea it's just one of those things you don't "see". :/ Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 9, 2012 Share Posted August 9, 2012 Yea it's just one of those things you don't "see". :/ I know, that people coming from Balkan Peninsula, they can not say all the true Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 11, 2012 Author Share Posted August 11, 2012 I'm from Balkan.. xD Anyways I'll keep you updated on the error if I find the answer. I'll get back to it now. (was doing some other things) Quote Link to comment Share on other sites More sharing options...
Ivan Ivković Posted August 17, 2012 Author Share Posted August 17, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.