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 6593645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:42:31+00:00 2026-05-25T17:42:31+00:00

I have a file that corrals my re-usable functions into one file ( functions.php

  • 0

I have a file that corrals my re-usable functions into one file (functions.php). It’s include_once()‘d on every page that needs it. I’m getting an error when my custom functions are trying to access a MySQL connection outside their own scope. The source is a bit like this:

<?php
    // functions.php
    $connect = mysql_connect("localhost", "user", "pass") or die("MySQL said: ".mysql_error());
    mysql_select_db("database", $connect) or die("MySQL said: ".mysql_error()); // no error

    /* ... */

    function getmotd($user) {           
        $query = "SELECT cid FROM `users`
        WHERE id = ".$user;
        $query = mysql_query($query, $connect); // error occurs here, $connect is not a valid MySQL link-resource
        /* ... */
    }
?>

Why can’t my function access variables declared above it’s scope? I can get a successful connection by reproducing $connect‘s declaration within the function.

Any insight into how I can work around this or what I’m doing wrong here?

  • 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-25T17:42:32+00:00Added an answer on May 25, 2026 at 5:42 pm

    Use the global keyword.

    Example

    function getmotd($user) {  
         global $connect;
        $query = "SELECT cid FROM `users`
        WHERE id = ".$user;
        $query = mysql_query($query, $connect); // error occurs here, $connect is not a valid MySQL link-resource
        /* ... */
    }
    

    You can also do it like this

    function getmotd($user) {  
        $query = "SELECT cid FROM `users`
        WHERE id = ".$user;
        $query = mysql_query($query, $GLOBALS['connect']); // error occurs here, $connect is not a valid MySQL link-resource
        /* ... */
    }
    

    If you want to make re-usable codes, you’d probably be better off with OOP. Create a class for the database, and add some properties for the database info, and access them from the functions by using the this keyword.

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

Sidebar

Related Questions

I have a file that needs to be refreshed every days, linked into a
I have a file that is organized into columns, with data that needs to
I have a file that has one entry per line. Each line has the
I have a file that I read into a char array. The char array
I have a file that needs a semicolon delimiter after the first field, which
I have a file that describes input data, which is split into several other
I have a file that is '|' delimited. One of the fields within the
I have a file that require()'s a namespace, as such: <?php require_once('Beer.php'); // This
I have a file that's been modified recently. Rather than looking into each revision,
I have a file that I want to include in Python but the included

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.