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!!!