Jump to content

Alternative to "Include" inside while loop


learningprobs

Recommended Posts

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 31

Warning: mysqli_stmt::free_result(): Couldn't fetch mysqli_stmt in /home/xxx/public_html/index.php on line 51

Warning: 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.

Link to comment
Share on other sites

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.php
resources/templates/frontend/video_category_2.php
resources/templates/frontend/video_category_3.php
resources/templates/frontend/video_category_4.php
resources/templates/frontend/video_category_5.php
resources/templates/frontend/video_category_6.php
resources/templates/frontend/video_category_7.php
resources/templates/frontend/video_category_8.php
resources/templates/frontend/video_category_9.php
resources/templates/frontend/video_category_10.php
resources/templates/frontend/video_category_11.php
resources/templates/frontend/video_category_12.php

I then pasted the path into my url:
website.com/resources/templates/frontend/video_category_12.php

I 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 by learningprobs
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

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.