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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:48:14+00:00 2026-06-05T08:48:14+00:00

I’ve read a LOT of things about this problem, however I still cannot fix

  • 0

I’ve read a LOT of things about this problem, however I still cannot fix it.

In my functions file I declare a variable with a value like so:

$px_host = "localhost";

And I have a database query function like so:

function dbQuery($database, $reqquery){
if(!$connect = mysql_connect($px_host, $px_dbuser, $px_dbpass)){
    exit("Error - cannot connect to MySQL server - " . mysql_error());
}

if(!$database = mysql_select_db($database)){
    exit("Error - cannot select database - " . mysql_error());
}

if(!$query = mysql_query($reqquery)){
    exit("Error - query error.");
}

return $query;
}

And whenever I try and run the function, I get an ‘Undefined Variable’ error. I’ve tried setting the variable to global, however it still says it’s undefined. They are in the same file and the variable is defined and set before the function.

(I’m bad at explaining this so try and work round it)
The file which contains dbQuery() is included in another file. In the other file, there is a function which uses dbQuery() to get certain information. Is it possible that because it’s being initially run from the first file, the variable is outside the scope?

  • 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-06-05T08:48:16+00:00Added an answer on June 5, 2026 at 8:48 am

    In your specific case you should declare global within function all variables outside the function. So

    function dbQuery($database, $reqquery){
        global $px_host,$px_dbuser,$px_dbpass;
        // rest of function
    }
    

    But your code can be improved: You should define constants at the beginning of your script

    define('DB_HOST','localhost');
    define('DB_USER','user');
    define('DB_PASS','pass');
    

    and use constants inside the function (so no need to declare global, and it’s more logic because host, user and pass aren’t variable but constants).

    You should connect at database only once at the beginning of your flow so the function dbQuery execute only the query (according with the function name).

    EDIT for completeness of answer:

    As some users say you in other comments, I invite you to read the php doc for mysql_connect and see the red advise:

    Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

    I’m not here for say you what you MUST do in your project, but read the doc and follow the tipps/suggestions is essential for the success of your project. 🙂

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

Sidebar

Related Questions

I am using parse_ini_file to read the contents of a file however it is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.