SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trouble passing db connection to cart class

    This is proving to be more difficult than it should, I think .
    I have an instantiated MySQL class assigned to a variable that I am trying to pass to my shopping cart class. The MySQL class is Harry F's from the PHP Anthology.

    PHP gives me this error:
    PHP Code:
         Fatal errorCall to a member function on a non-object in  /home/tandrtrailer.com/htdocs/exp/lib/Cart.php on line  142 
    In my included config file, I have this, in part:
    PHP Code:
         $CFG->dbhost "localhost";
         
    $CFG->dbname "myDb";
         
    $CFG->dbuser "myLogin";
         
    $CFG->dbpass "myPass";
         require_once ( 
    "Session.php" );
         require_once ( 
    "MySQL.php" );
         
         
    $session = new Session;
         
         
    /** Instantiate new MySQL object **/
         
    $db = &new MySQL $CFG->dbhost$CFG->dbuser,
                                  
    $CFG->dbpass$CFG->dbname );
         
         if (! isset ( 
    $_SESSION["cart"] ) ) {
             
    $_SESSION["cart"] = new Cart ( &$db );
         } 
    As you can see, I'm trying to pass a reference to the instance of MySQL contained in $db to $_SESSION["cart"]. However, when I try to then do this:
    PHP Code:
         function calculateTotal ()
         {
         ....
         
    $sql "SELECT id, price, weight FROM products
                 WHERE id IN (
    $idsInCart)";
       
         
    $result $this->db->query $sql );
         
         while ( 
    $product mysql_fetch_object $result ) ) {
        
    $this->total += $this->items[$product->id] * $product->price;
        
    $this->weight += $this->items[$product->id] * $product->weight;
           } 
    //end while
         

    ...I get the error mentioned above.
    Am I not passing the $db connection correctly? What do you guys think?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wrong...
    PHP Code:
     $result $this->db->query $sql ); 
    right...
    PHP Code:
     $result $this->$db->query $sql ); 
    couse $db is a variable which u get by the function which define the type of db!
    but i see that u use mysql so write it $this->mysql_query($sql);
    cheers

  3. #3
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help Reminder! . However, with the above examples, I got an Undefined variable error using:
    PHP Code:
    $result $this->$db->query $sql ); 
    The other example resulted in an error stating the the function mysql_query was and undefined function.

    Let me post more of my cart class so that you can see how I'm trying to do this.
    PHP Code:
      class Cart {
          var 
    $db;  /* Instance of MySQL class */
          
    var ....    /* more variable declarations */
          
          
    function Cart ( &$db )
          {
            
    $this->db = &$db ;
            
    $this->init ();  //calls function that resets all vars to "0"
          

    But this is obviously not working. My goal here is to make one database connection like:
    PHP Code:
      $dbConn = &new MySQL $dbUser$dbPass$dbHost$dbName); 
    that i can pass around to any of my included classes in order to simplify mySql database usage. Any suggestions?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  4. #4
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $db = &new MySQL $CFG->dbhost$CFG->dbuser,
                                 
    $CFG->dbpass$CFG->dbname );
     
    if (! isset ( 
    $_SESSION["cart"] ) ) {
        
    $_SESSION["cart"] = new Cart ( &$db );

    PHP Code:
    if (! isset ( $_SESSION["cart"] ) ) {
        
    $_SESSION["cart"] = new Cart;
    }
    $CART =& $_SESSION['CART'];
    //-- which makes it easy for eg $CART->deleteItem, $CART->addItem ... 
    PHP Code:
    function calculateTotal ()
    {
    ....

    PHP Code:
    function calculateTotal ()
    {
        global 
    $db;
     
        
    //....

    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  5. #5
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frezno
    PHP Code:
       function calculateTotal ()
       {
           global 
    $db;
        
           
    //....
       

    Your first suggestion was great. I just implimented it! Thanks frezno.

    Correct me if I'm wrong, but if you declare a var in a class, that variable becomes global within all class methods, right?
    So if I have this:
    PHP Code:
       class Cart {
           var 
    $db
       
    //.....
          /* contructor */
         
    function Cart ( &$db )
         {
          
    $this->db = &$db;
          
    //...
         
    }
      
         
    /***** Methods ******/
      
        //...
      
       
    // end Class Cart 
    ..then it should be accessible by all methods, thus becomming global in scope for that class, like:
    PHP Code:
       function calculateTotal ()
       {
         
    //....
         
    $result $this->db->query $sql ); 
    I shouldn't need to use:
    PHP Code:
    global $db
    in a class method, should I?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  6. #6
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by garlinto
    but if you declare a var in a class, that variable becomes global within all class methods, right?
    Yes.

    But you don't want to have database connection and cart functions within one class.
    Those are two separate classes.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •