Jump to content

classes and mysql connections...again


littlened

Recommended Posts

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.

Link to comment
Share on other sites

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 connection

Warning: 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 13
Error with query:

SELECT * FROM Products


Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.