Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting to two databases
    text
    copied!<p>I have an application in which I want to authenticate a user from a first database &amp; manage other activities from another database. I have created two classes. An object of the classes is defined in a file:</p> <pre><code>$objdb1=new db1(),$objdb2=new db2(); </code></pre> <p>But when I try to call <code>$objdb1-&gt;fn()</code>. It searches from the <code>$objdb2</code> &amp; is showing table1 doesnot exists?</p> <p>My first file database.php</p> <pre><code>class database { private $hostname; private $database; private $username; private $password; private $dblinkid; function __construct() { if($_SERVER['SERVER_NAME'] == 'localhost') { $this-&gt;hostname = "localhost"; $this-&gt;database = "aaaa"; $this-&gt;username = "xxx"; $this-&gt;password = ""; } else { $this-&gt;hostname = "localhost"; $this-&gt;database = "xxx"; $this-&gt;username = "xxx"; $this-&gt;password = "xxx"; } $this-&gt;dblinkid = $this-&gt;connect(); } protected function connect() { $linkid = mysql_connect($this-&gt;hostname, $this-&gt;username, $this-&gt;password) or die("Could not Connect ".mysql_errno($linkid)); mysql_select_db($this-&gt;database, $linkid) or die("Could not select database ".mysql_errno($linkid)) ; return $linkid; } </code></pre> <p>Similarly second file</p> <pre><code>class database2 { private $vhostname; private $vdatabase; private $vusername; private $vpassword; private $vdblinkid; function __construct() { if($_SERVER['SERVER_NAME'] == 'localhost') { $this-&gt;vhostname = "xxx"; $this-&gt;vdatabase = "bbbb"; $this-&gt;vusername = "xxx"; $this-&gt;vpassword = ""; } else { $this-&gt;vhostname = "localhost"; $this-&gt;vdatabase = "xxxx"; $this-&gt;vusername = "xxxx"; $this-&gt;vpassword = "xxxx"; } $this-&gt;vdblinkid = $this-&gt;vconnect(); } protected function vconnect() { $vlinkid = mysql_connect($this-&gt;vhostname, $this-&gt;vusername, $this-&gt;vpassword) or die("Could not Connect ".mysql_errno($vlinkid)); mysql_select_db($this-&gt;vdatabase, $vlinkid) or die("Could not select database ".mysql_errno($vlinkid)) ; return $vlinkid; } </code></pre> <p>Third file</p> <pre><code>$objdb1 = new database(); $objdb2 = new database2(); </code></pre> <p>Can you help me on this?</p> <p>Regards,</p> <p>Pankaj</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