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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:40:42+00:00 2026-06-05T13:40:42+00:00

Okay, so I have the weirdest problem right now. My code is fine! It

  • 0

Okay, so I have the weirdest problem right now. My code is fine! It works PERFECTLY as I want and expect it to, which is a major shock to me, because every time I go to do MySQL I get endless problems that I can never find a solution to.

But today, everything works perfectly. So what’s the problem? The problem is that when I try to wrap my code in a function, it just… stops working! I have error checking after every step and it works fine when in it’s own file and lines. But I need to put it in a function so I can add parameters (hence the “extraQuery” variable), and “return” it so that I can access it from a separate file.

Here is my base code:

$link = mysql_connect($db_host, $db_user, $db_pass) or die ("Unable to connect: " . mysql_error());
mysql_select_db($db_name, $link) or die("Couldn't connect to database: " . mysql_error());

$extraQuery = "";
$result = mysql_query("SELECT * FROM things " . $extraQuery . " ORDER BY RAND() LIMIT 1", $link);

$row = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_close($link);
echo($row['content']);

As I said, it works completely fine as that is. But when I do this….

function getResult(){
    $link = mysql_connect($db_host, $db_user, $db_pass) or die ("Unable to connect: " . mysql_error());
    mysql_select_db($db_name, $link) or die("Couldn't connect to database: " . mysql_error());

    $extraQuery = "";
    $result = mysql_query("SELECT * FROM things " . $extraQuery . " ORDER BY RAND() LIMIT 1", $link);

    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    mysql_close($link);
    echo($row['content']);
}
getResult();

..It stops working at the second line, telling me “No database selected”. But as you can see, I am explicitly selecting a database that DOES exist and otherwise works when it’s not in a function.
And just to stress this, it is NOT my user permissions, nor is it the name of the database; because it works perfectly when outside of the function.

Any possible aid would be greatly appreciated.

  • 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-05T13:40:43+00:00Added an answer on June 5, 2026 at 1:40 pm

    Due to variable scope your database variables are not available inside of the function. You need to pass it as an argument for getResult() to have access to it:

    function getResult($db_host, $db_user, $db_pass, $db_name, $extraQuery){
        $link = mysql_connect($db_host, $db_user, $db_pass) or die ("Unable to connect: " . mysql_error());
        mysql_select_db($db_name, $link) or die("Couldn't connect to database: " . mysql_error());
    
        $extraQuery = "";
        $result = mysql_query("SELECT * FROM things " . $extraQuery . " ORDER BY RAND() LIMIT 1", $link);
    
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        mysql_close($link);
        echo($row['content']);
    }
    getResult();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so i have a semi weridish problem with re.sub. Take the following code:
Okay I have an extensive ruby application running behind Ruby on Rails right now.
Okay I have the following problem. I want to get the current dateTime and
Okay I have the below code performing something I don't want it to do.
Okay so I have the following Code which appends a string to another in
Okay I have a series of objects based on a base class which are
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly
Okay -- we have an interesting problem. Some background: Our main application uses Indy
okay so i have a div and a span right next to each other

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.