Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch data from second database frequently while connection to first database is always available
    text
    copied!<p>I m working on old existing project which uses <code>mysql</code> function for database operation. The existing system connects to the database, say <code>cdcol</code>. The connection to this database is available through site wise. Now I want to fetch data from another database say <code>crawlerdb</code>, assign fetched data to an array and close connection to this database. The connection to second database is inside a function say <code>GetAccess</code>, and each time the extra data needed, the function is called, data fetched and connection closed to the second database. All I want is connection to first database should be available every time.</p> <p>The problem I m facing is. If i don't close connection to second database. Then mysql query used after calling the function <code>GetAccess</code>, still search items from second database, because the connection to second database is active. If I close the connection to second database, still the query doesnot work. Following code explains my situation.</p> <pre><code>&lt;?php //$conn1 is permanent connection that is used sitewise. $conn1=mysql_connect("localhost","root","",true) or die(mysql_error()); mysql_select_db("cdcol",$conn1) or die(mysql_error()); echo "1. Current Database = ".mysql_current_db();//prints cdcol echo "&lt;Br&gt; Function Returned Value = ".GetAccess(); echo "&lt;Br&gt;2. Current Database = ".mysql_current_db(); //In GetAccess function, which is called above if mysql_close($conn2) is used, the mysql_current_db() returns empty value. //A FUNCTION TO GET EXTRA DATA FROM SECOND DATABASE function GetAccess(){ $conn2=mysql_connect("localhost","root","",true) or die(mysql_error()); mysql_select_db("crawlerdb",$conn2) or die(mysql_error()); $test=mysql_query("select * from tbllensinfo",$conn2); //here i have used $conn2 as link identifier $var= mysql_num_rows($test); mysql_close($conn2); return $var; } //FUNCTION TO IDENTIFY WHICH DATABASE IS CURRENTLY BEING USED function mysql_current_db() { $r = mysql_query("SELECT DATABASE()") or die(mysql_error()); return mysql_result($r,0); } $res=mysql_query("select * from cds"); //here link identifier $conn1 is not used, i cant change this code because there are several 100s codes, so not possible to change in all of them. Everything will work if $conn1 is used here though echo "&lt;br&gt;".mysql_num_rows($res); ?&gt; </code></pre> <p><strong>NOTE:</strong> The two database are hosted on same server, but database users are different, one of which have no access to other database.</p> <p>So in short What I need is I need to fetch data from second database frequently while connection to first database is always available.</p> <p>Any help will highly be appreciable, thanks !</p> <p>Thanks Sharmila</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload