0

i have two classes that are almost identical, they work independently but not when both on the same page.

It seems to give an odd output error, combining the two classes.

Have i made an error here?

$db = new query("localhost","xxxxxx","xxxxxx","xxxxxx");

class query extends hush { 
    public function __construct($host,$user,$pass,$dbname) {
        mysql_connect($host,$user,$pass)
            or die(mysql_error());
        mysql_select_db($dbname)
            or die(mysql_error());
    }
    public function qry($sql) {
       // return mysql_query($sql);
            $query = mysql_query($sql) or die(mysql_error());
            return $query;  
    }   

} 



$dbx = new queryx("localhost","xxxxxx","xxxxxx","xxxxxx");

class queryx extends hush { 
    public function __construct($host2,$user2,$pass2,$dbname2) {
        mysql_connect($host2,$user2,$pass2)
            or die(mysql_error());
        mysql_select_db($dbname2)
            or die(mysql_error());
    }
    public function qry2($sql2) {
       // return mysql_query($sql);
            $query2 = mysql_query($sql2) or die(mysql_error());
            echo $query2;  
    }

} 
Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
Derple
  • 865
  • 11
  • 28
  • 1
    You're using mysql_* functions without a link identifier. Meaning you are overwriting your previous connection. First, stop using mysql_* functions, they are deprecated, second use objects with pdo or mysqli or a link identifier and you won't have this issue. – Devon Bessemer May 10 '15 at 05:29
  • i dont follow, could you explain please? – Derple May 10 '15 at 05:30
  • 1
    You might be interested in this: [Why you shouldn't use mysql_* functions in php](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). There are alternatives listed, go for them. – gvlasov May 10 '15 at 05:30
  • 1
    @StuartWickenden you call mysql_connect twice. How would it know which mysql connection you are wanting to use for mysql_query ? – Devon Bessemer May 10 '15 at 05:32

1 Answers1

1

Here's a procedural example using mysqli. However, since you're using classes, I think you should try to use objects.

class query extends hush { 

    protected $db;

    public function __construct($host,$user,$pass,$dbname) {
        $this->db = mysqli_connect($host,$user,$pass,$dbname)
    }
    public function qry($sql) {
        return mysqli_query($this->db, $sql) or die(mysqli_error());
    }

} 

class queryx extends hush { 

    protected $db;

    public function __construct($host,$user,$pass,$dbname) {
        $this->db = mysqli_connect($host,$user,$pass,$dbname)
    }
    public function qry($sql) {
        return mysqli_query($this->db, $sql) or die(mysqli_error()); 
    }

} 

Notice I have used a class property for the link identifier ($this->db). This will prevent any conflict from multiple connections.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95