I'm trying to connect to a database within the context of a class. My database connect info is held in one file and I am calling it from a class within two different methods of the class. The database call works properly for the first method, but does not for the second method. Particularly, in the database connect file, I define a variable $dbc like so

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

Of course, earlier in the file, all of the necessary variables are defined.

Here is an example of the structure of the class:

<?php

class Jewelry_gallery {

public $jewelry;
public $num_pics;
public $script;
private $rows = 5;

    public function __construct($jewelry, $num_pics) 
{
    }

public function display_thumbs()
{				
}

public function main_picture()
{
    }
}
?>

I try calling the database connect file from both the display_thumbs and main_picture methods using a require_once function. Now, everything executes correctly within the display_thumbs method (which is called first in the files references this class), but I get an error like this when the main_picture attempts o execute: "Notice: Undefined variable: dbc in /***/classes/jewelry_gallery.php on line 143". My guess is that it's an issue of scope. Since I intially call the database connect information in display_thumbs, the $dbc variable cannot be referenced outside of that method. I tried to set it as a global variable, but that did not help either. Additionally, I tried redefining the variable for the main_picture method, but neither seem to work.

Any suggestions? I tried to may the code simple by getting rid of a lot of clutter, but if you want to see the actual code, let me know.

Thanks so much for the help!!!

    Set it as a class property:

    class Whatever
    {
        var $dbc = '';
    
    function __construct()
    {
         if ($this->dbc === '')
         {
              // Connect $this->dbc to the db
         }
    }
    }
    

    Then just use the connection as a property. Note that if you serialize the object, the connection is severed. Check out the php magic method __wakeup() to help that problem though.

    Good luck!

      Excellent idea...I just cannot get the $dbc variable from the mysql_connect.php file to connect to the $this->dbc variable. Here's what I'm doing:

      <?php
      
      class Jewelry_gallery {
      
      public $dbc = '';
      
          public function __construct($jewelry, $num_pics) 
      {
                  if($this->dbc === '')
                  {
      	       require_once('includes/mysql_connect.php');
      	       $this->$dbc = $dbc;
                   }
          }
      }
      ?>

      I've tried using both include and require_once to connect the script...is that an issue? Here is the script I'm connecting to:

      <?php
      define ('DB_USER', '....');
      define ('DB_PASSWORD', '....');
      define ('DB_HOST', '....');
      define ('DB_NAME', '....');
      
      $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
      ?>

      I appreciate your help. Can you help me finish this thing off?

        Ok...I got it now. Rather than calling the mysqli_connect function in the mysql_connect.php file, I call it in the class and assign it to $this->dbc.

        Thanks for all of the help.

          Write a Reply...