Jump to content



Photo

PHP Multiple Database Connections causing issues


  • Please log in to reply
3 replies to this topic

#1 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 13 February 2013 - 17:28

Okay, so I am working on a website that will use 2 database connections. The connection class looks like:

<?php
class connection {
// variables to store the database information
private $host;// = "localhost";
private $username;// = "root";
private $password;// = "";
private $database;// = "";

//variables to store database connection
private $link;
private $result;
public $query;


//constructer
function __construct($db, $server = "localhost",$user = "root",$pass="")
{
  $this->host = $server;
  $this->username = $user;
  $this->password = $pass;
  $this->database = $db;
 
  $this->link = mysql_connect($this->host,$this->username, $this->password);
  mysql_select_db($this->database,$this->link);
  return $this->link;
}
function Query($query)
{
  if(!empty($query)) {
   $this->query = $query;
   $this->result = mysql_query($this->query,$this->link);
   return $this->result;
  }else {
   return false;
  }
}

function Fetch($result="")
{
  if (empty($result)) {$result = $this->result;}
  return mysql_fetch_assoc($result);
}

}
?>

I create two objects in my index.php file:

<?php
session_start();

include('website/includes/connection.php');
$progdb = new connection("progdb");
$coredb = new connection("coredb");
?>

If I do something like:

$query = "SELECT Count(skuID) FROM sku";
$result = $coredb->Query($query);
if ($result & mysql_num_rows($result) > 0)
{
  //CODE
}

It works, however doing the same with the progdb (with proper tables) does not, the Query function returns false.

BUT... if I swap the order in which they are created (so $coredb before $progdb) then the opposite is true, the progdb queries work but the coredb queries fail.

What am I doing wrong? I hope I have provided enough information. I am stumped.


#2 Tekkerson

Tekkerson

    Droppin' them beats like hot potatoes.

  • Joined: 05-March 08
  • Location: Dallas, TX

Posted 13 February 2013 - 17:34

http://stackoverflow...-single-webpage

Here you go.

Although if I may suggest you should be using PDO since it's more secure and would work with future PHP versions. Since mysql_connect() is going to be removed in the future.

#3 tim_s

tim_s

    Default

  • Tech Issues Solved: 1
  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: iPhone 5s

Posted 13 February 2013 - 17:39

I came to help but looked like the answer is already posted within the link.

#4 OP firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 13 February 2013 - 17:53

Awesome thanks for the help guys, it's working perfectly now!