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

  • SEARCH
  • Home
  • 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 7607423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:39:29+00:00 2026-05-31T00:39:29+00:00

Everything was working fine in my little project, until I decided to clean up

  • 0

Everything was working fine in my little project, until I decided to clean up a little bit and moved database-related php-files to their own folder. Then things went strange.

I am trying to use two functions here:

function getEntries () {
    require_once("mysqliVariables.php");
    $mysqli = new mysqli($dbHost, $dbUname, $dbPwd, $dbName);           
    $sql = "statement...";
    $result = $mysqli->query($sql) or die($mysqli->error);

    echo $dbHost; // prints host
    return $result;
}

function getBiggestMonth () {
    require_once("mysqliVariables.php");
    $mysqli = new mysqli($dbHost, $dbUname, $dbPwd, $dbName);           
    echo $dbHost; // prints nothing! why?       

    $sql = "statement...";
    $result = $mysqli->query($sql) or die($mysqli->error); // this line does not run, of course.

    return $result;
}

I use another function in a different file (and folder) to call these functions, that starts like this:

function listTasks() {      
    require_once("db/mysqliFunctions.php");

    // Get entries using mysqli.
    $tasks = getEntries();

    echo "<pre>";
    var_dump($tasks);
    echo "</pre>"; // program works fine this far.

    $bm = getBiggestMonth(); // program breaks somehow during this function call.

My variables are in a php-file like so:

<?php   
$dbHost = "host";
$dbUname = "username";
$dbPwd = "password";
$dbName = "databasename";

?>

If I switch the funtion’s call order, then getBiggestMonth() runs fine and the other one won’t. Also, all of this worked fine when all the files were located in the same folder (the functions were then static functions inside a class, but that shouldn’t be an issue, the same problem persists here), so I dont understand how possible variable scope can be different here, and require_once should take care of other things. Help?

  • 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-31T00:39:31+00:00Added an answer on May 31, 2026 at 12:39 am

    This is because you are using require_once. It will only include the configuration once. You can change it to use require so that it will work as you expect.

    The require_once() statement is identical to require() except PHP will
    check if the file has already been included, and if so, not include
    (require) it again.

    You are using require_once to pull in a file into the scope of the getEntries() function. PHP keeps a record of the files that have been required in so when you then call require_once in getBiggestMonth() it knows it has already been included in getEntries(). Because it has already been included it does not require the file in again so you don’t get your variables in your getBiggestMonth() scope.

    require_once does not have anything to do with variables it just monitors the files that have been included into the current PHP process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everything is working fine until I call the saveFile method (shown below) to write
I'm tring to setup a little chat program in python. Everything was working fine
I am sending email from my iPhone application. Everything working fine, but I want
everything is working fine with my jqgrid except a small issue. i have defined
I'm working on a localized app and everything is working fine. The problem is
Here is my code, everything is working fine the only problem is with the
In my application comes with an uninstaller. Everything is working fine, except that I
I have an asp:FormView control bound to a datasource. Everything is working fine. If
I have a bunch of self-hosted WCF services. Everything's working fine but I'm look
Everything was working just fine and on the last publish my wordpress directory is

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.