learningprobs Posted September 6, 2016 Share Posted September 6, 2016 Hello, Today for the first time I tried to use include() inside a while loop, to my surprise, it does not work. The error message I am receiving is: Warning: mysqli_stmt::fetch(): Couldn't fetch mysqli_stmt in /home/xxx/public_html/index.php on line 31Warning: mysqli_stmt::free_result(): Couldn't fetch mysqli_stmt in /home/xxx/public_html/index.php on line 51Warning: mysqli_stmt::close(): Couldn't fetch mysqli_stmt in /home/xxx/public_html/index.php on line 52 Here is my code: <?php $video_activated = 1; $stmt = $conn->prepare("SELECT video_categories_id FROM video_categories ORDER BY video_categories_id, video_activated = ?"); $stmt->bind_param('i', $video_activated); $stmt->bind_result($video_categories_id); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows() > 0){ while($stmt->fetch()) { if($video_categories_id == 1){ include(TEMPLATE_FRONT . DS . "video_category_1.php"); } if($video_categories_id == 2){ include(TEMPLATE_FRONT . DS . "video_category_2.php"); } if($video_categories_id == 3){ include(TEMPLATE_FRONT . DS . "video_category_3.php"); } if($video_categories_id == 4){ include(TEMPLATE_FRONT . DS . "video_category_4.php"); } }} $stmt->free_result(); $stmt->close(); ?> All I am trying to do is load my gallery pictures according to the database categories status being either activated or not. Any idea why Include does not work in this case please? If I replace the include() by echo, it works. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/ Share on other sites More sharing options...
maxxd Posted September 6, 2016 Share Posted September 6, 2016 The problem isn't with the include statement, it's with the SQL query. You've got a comparison in your ORDER BY clause. I think you want SELECT video_categories_id FROM video_categories WHERE video_activated = ? ORDER BY video_categories_id Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537117 Share on other sites More sharing options...
learningprobs Posted September 6, 2016 Author Share Posted September 6, 2016 (edited) Sorry Max this is my latest code, it is driving me nuts, I have been on it for hours. People in another forum are saying that my database connection might be the problem but it does not make sense, why would I get the echo to work on that variable $video_categories_id and not on the require statement? I only get the stmt error via the require and not the echo(using both the same variable) PHP Code: <?php$video_activated = 1;$stmt = $conn->prepare("SELECT video_categories_id FROM video_categories ORDER BY video_categories_id, video_activated = ?");$stmt->bind_param('i', $video_activated);$stmt->bind_result($video_categories_id);$stmt->execute();$stmt->store_result();if($stmt->num_rows() > 0){while($stmt->fetch()) {require 'resources/templates/frontend/video_category_' . $video_categories_id . '.php';}}$stmt->free_result();$stmt->close();?> If I change PHP Code: require 'resources/templates/frontend/video_category_' . $video_categories_id . '.php'; to PHP Code: echo 'resources/templates/frontend/video_category_' . $video_categories_id . '.php'; I get everything fine:resources/templates/frontend/video_category_1.phpresources/templates/frontend/video_category_2.phpresources/templates/frontend/video_category_3.phpresources/templates/frontend/video_category_4.phpresources/templates/frontend/video_category_5.phpresources/templates/frontend/video_category_6.phpresources/templates/frontend/video_category_7.phpresources/templates/frontend/video_category_8.phpresources/templates/frontend/video_category_9.phpresources/templates/frontend/video_category_10.phpresources/templates/frontend/video_category_11.phpresources/templates/frontend/video_category_12.phpI then pasted the path into my url:website.com/resources/templates/frontend/video_category_12.phpI could see the output, therefore my path is fine.There is something(no idea if it is because of the prepared statements) that do not let that "require" pass through in my fetch()Anything else works but not when I add "require" in my while($stmt->fetch()) {} Edited September 6, 2016 by learningprobs Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537118 Share on other sites More sharing options...
ginerjm Posted September 6, 2016 Share Posted September 6, 2016 Can we see the actual file being brought in? Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537119 Share on other sites More sharing options...
learningprobs Posted September 6, 2016 Author Share Posted September 6, 2016 you are a master ginerjm...........!!!!!!!!!!!!!! You just asked me this question "Can we see the actual file being brought in? " then I thought...." Oh no!!!!!!!". Well you are right, the problem is from the other end, it is the files themselves having a PHP error in them. Well done, I have been turning around in circle for a solution in another forum, noone asked this question when in fact it was the most logical thinking. Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537120 Share on other sites More sharing options...
ginerjm Posted September 6, 2016 Share Posted September 6, 2016 I am far from a master. At least not at PHP. It was simply a matter of continuing to search for the problem. Actually I wanted to see if your required input was a function or some other code that could not be executed by simply being included. Or if you had php tags in it. Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537121 Share on other sites More sharing options...
ginerjm Posted September 6, 2016 Share Posted September 6, 2016 If you are happy that you have solved this topic, you should mark it closed. Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537122 Share on other sites More sharing options...
Barand Posted September 6, 2016 Share Posted September 6, 2016 The problem isn't with the include statement, it's with the SQL query. You've got a comparison in your ORDER BY clause. @maxxd, That is not a problem. Consider this mysql> SELECT firstname -> , lastname -> , gender -> FROM employees -> ORDER BY lastname; +-----------+----------+--------+ | firstname | lastname | gender | +-----------+----------+--------+ | Emily | Bronte | F | | Jane | Doe | F | | Jack | Evans | M | | Patricia | Jones | F | | Davy | Jones | M | | John | Smith | M | +-----------+----------+--------+ Suppose that, for some weird reason, we wanted all those whose first name began with 'J' to be listed first. mysql> SELECT firstname -> , lastname -> , gender -> FROM employees -> ORDER BY SUBSTRING(firstname,1,1)='J' DESC, lastname; +-----------+----------+--------+ | firstname | lastname | gender | +-----------+----------+--------+ | Jane | Doe | F | | Jack | Evans | M | | John | Smith | M | | Emily | Bronte | F | | Patricia | Jones | F | | Davy | Jones | M | +-----------+----------+--------+ The comparison in the ORDER BY clause evaluates to true(1) or false(0) Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537123 Share on other sites More sharing options...
maxxd Posted September 7, 2016 Share Posted September 7, 2016 @barand - I didn't think of that. Honestly, didn't even know that would work - thanks! I love learning something new. Quote Link to comment https://forums.phpfreaks.com/topic/302090-alternative-to-include-inside-while-loop/#findComment-1537135 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.