Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:06:01+00:00 2026-05-13T15:06:01+00:00

I have a large PHP loop that I’m re-using in multiple spots on multiple

  • 0

I have a large PHP loop that I’m re-using in multiple spots on multiple pages of my site. I’d normally just use an include() to set that PHP loop wherever it’s needed, but the PHP loop in question makes use of a MySQL query in which I need to change variables (different filters and different LIMITs) for different instances of this PHP loop.

<?php 
    $results = mysql_query("SELECT * FROM databaseName ORDER BY columnName DESC, otherColumnName DESC LIMIT 6", $connection);
    if (!$results) {
        die("Database Query Failed");
    }
?>

<?php
    while ($variable = mysql_fetch_array($results)) {
        ----Execute this loop----
    };
?>

Which I then recalled wherever I needed it using

<?php include("/filepath/includedfile.php"); ?>

Everything was running fine until I took the actual MySQL query OUT of the included file and placed it before each instance of that included PHP (so I could control certain variables for each instance of the query). Now, the MySQL query and the included PHP loop don’t seem to be communicating the same way, and I’m getting the following warning:

“Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/filepathhere/includedfile.php on line 3″<

I feel like the included PHP is being run in the included file and THEN being injected into the main page. I need the code to be injected into the main page before it gets run.

Is there a better way than “include()” to inject that PHP loop into the main page?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:06:01+00:00Added an answer on May 13, 2026 at 3:06 pm

    Seems like a good time to learn about functions and function arguments. 🙂


    To elaborate: including a file is one way to reuse code, but as you are experiencing, there’s no controlled way of altering that code’s behavior. A function has a clearly defined interface to pass arguments into the function, which you can use inside the function to alter its behavior.

    function my_big_loop($condition) {
    
        // preparations
    
        $condition = mysql_real_escape_string($condition);  // always! even for simple examples. :)
        $results = mysql_query("SELECT * FROM somewhere WHERE x = '$condition'");
    
        // do loopy stuff
    
        return $big_pile_of_HTML;
    }
    
    echo my_big_loop('somecondition');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a very large PHP MVC-based site that will have a large
I have a large-ish PHP project (hundreds of files) that I've been brought in
I have a PHP file that contains a large array (about 90KB). I'm considering
I have a PHP script that creates large complex tables. I am attempting to
I have an HTML table that has rows added in a PHP loop. This
Using PHP , I would like to make a while loop that reads a
I am using gettext for a large PHP project. I have successfully setup everything,
I have a large PHP class that has a large variable/function declaration and I
I have a large PHP application. After I changed some settings I get a
I have a very large php maintenance script (basically it recreates thumbnails for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.