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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:10:43+00:00 2026-06-13T05:10:43+00:00

We open a MySQL connection to host_ 1 at the beginning of our script.

  • 0

We open a MySQL connection to host_1 at the beginning of our script. Somewhere in between, we call a function that opens & closes a connection to host_2 (function retrieve_information_from_host_2).

Unfortunately, the code after the function call still uses the connection of the function (host_2).

We need it though to continue with the preceding connection host_1.

<?php

function retrieve_information_from_host_2($host_2, $username_2, $password_2, $database_2) {
    $connection = mysql_connect($host_2, $username_2, $password_2);
    if($connection) {
        if(mysql_select_db($database_2)) {
            $sql = "SELECT * FROM table WHERE id = 1;";
            $erg = mysql_query($sql);
            $row = mysql_fetch_assoc($erg);

            return $row;
        }
        mysql_close($connection);
    }
}

if(mysql_connect($host_1, $username_1, $password_1)) {
    if(mysql_select_db($database_1)) {
        $sql = "SELECT * FROM table WHERE id = 1;";
        $erg = mysql_query($sql);
        $row = mysql_fetch_assoc($erg); # CORRECT - This returns data from host 1

        $row_host_2 = retrieve_information_from_host_2(); # CORRECT - This returns data from host 2

        $sql = "SELECT * FROM table WHERE id = 2;";
        $erg = mysql_query($sql);
        $row = mysql_fetch_assoc($erg); # WRONG - This returns data from host 2 instead of host 1
    }
    mysql_close();
}
?>

We have tried almost every combination by giving each connection a name

$connection_1 = mysql_connect($host_1, $username_1, $password_1);
...
mysql_close($connection_1);

and closing them explicitly, etc.

Nothing helps. Can someone give us a solution to this problem?

PS: We are bound to this approach (functions) and can’t use classes.

Thank you very much!

  • 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-13T05:10:44+00:00Added an answer on June 13, 2026 at 5:10 am

    Try to pass your connection link identifier as second argument.

    For example:

    $connection1 = mysql_connect($host_1, $username_1, $password_1);
    $connection2 = mysql_connect($host_2, $username_2, $password_2);
    mysql_query('Query',$connection1);
    mysql_query('Query',$connection2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If a MySQL database connection is already open from a script (say PHP ),
Using MySQL & Java Script I want to make a mysql connection in Java
When I open a MySQL connection in PHP with just PHP's built-in MySQL functions,
I have MySQL running such that I can open a client command line and
I have a mod_perl2 based web app that requires a connection to a mysql
When you open a MySQL connection with mysql_connect(), it returns a link identifier. But
I have a system with a globally open mysql connection to the local server.
for a project I need to open a Mysql connection, close it and let
Does anyone know any open source project or adapter for MySQL-AdWhirl? We try to
I'm working on a database with mysql 5.0 for an open source project it's

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.