littlened Posted January 26, 2007 Share Posted January 26, 2007 this is a class I have to initial a db connection[code]class DB { var $host = 'localhost'; var $user = 'dsfsdfsd'; var $pass = 'dfsdf'; var $dbase = 'dfsdfsdf'; var $conn; function DB() { $this->connect(); } function connect() { $this->conn = mysql_connect($this->host, $this->user, $this->pass); mysql_select_db($this->dbase, $this->conn); }}[/code]How can I use this class to create the db connection at the beginning of a php file, and then use the same connection inside functions within other classes?I tried initiating the DB connection from within each class, but now I've reached a point where I am including 2 classes in a php file and both classes are trying to create an object from the DB class and php wont allow it. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 use pconnect instead of connect. Do you get an error? What is it? Quote Link to comment Share on other sites More sharing options...
littlened Posted January 26, 2007 Author Share Posted January 26, 2007 this is the error, it says error with queyr, but to me the query seems fine and the products table does exist, so I assume its the connectionWarning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/alive/public_html/clients/zund/www/manager/classes/cls_products.php on line 13Error with query:SELECT * FROM Products Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 Did you try it using pconnect?How are you calling the class?You didn't post your query function either. Quote Link to comment Share on other sites More sharing options...
littlened Posted January 26, 2007 Author Share Posted January 26, 2007 [quote]class categories { var $db; function categories() { include 'classes/cls_db.php'; $this->db = new DB(); } function get_categories() { $query = "SELECT Category_ID,Category_Name,Category_Display FROM Categories ORDER BY Category_Name ASC "; $result = mysql_query($query, $this->db->conn) or die("Error with query:<br /><pre>{$query}</pre><br /><br >" . mysql_error()); $num_rows = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { $categories[] = $row; } return $categories; } }[/quote]thats one class which uses the DB connection, then also have another class which I've tried to do in exactly the same way, however when I include 2 classes in a php file where both classes need to use the DB class, I get an error about not being able to initiate the class because it's already been done, so obviously I can't create the DB class twice. So what I need is a way to create the db class from a php file, then include 2 classes which can access the connection setup in the db class, without having to try and create 2 instances of the db class.hope that makes sense. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 So before including any of these files, make a global $db - then instead of using $this->db, just use $db; Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 26, 2007 Share Posted January 26, 2007 just extend your classes. you are using php5 right?to use the methods in the parent class, just say $this->method()class categories extends DB{public function __construct(){parent::__construct(); //in change your db method to __construct(){}}methods...} Quote Link to comment Share on other sites More sharing options...
trq Posted January 27, 2007 Share Posted January 27, 2007 Don't extend the database class with a catigories class, that is just poor design. All you need do is NOT pass the second argument to mysql_query. eg;[code]<?php $db = new DB(); mysql_query("SELECT * FROM foo");?>[/code]Your problem is that your DB object is NOT a connection resource. Like I said before, it looks to me like you are trying to run before you can walk. Why are you using classes at all when it doesn't really seem you have the basics of procedural php down yet? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.