Jump to content

how to make db config connection available throughout entire app?


ballhogjoni

Recommended Posts

I want to be able to connect to my db throughout my app, even in classes, but can't. Any ideas?

 

config.php

$oZend_Db_Adapter_Pdo_Mysql = Zend_Db::factory('Pdo_Mysql', array(
    	'host'     => 'localhost',
    	'username' => 'root',
    	'password' => '',
    	'dbname'   => 'xxxxxx'
	)
);

 

class IndexController extends Zend_Controller_Action{
    
function indexAction(){
	$this->view->base_url = BASE_URL;
}

function addressAction(){
	$oAuthNamespace = new Zend_Session_Namespace('Zend_Auth');
	$this->view->base_url = BASE_URL;
	if($_POST){
		$oAuthNamespace->post = $_POST;
		foreach($_POST as $key=>$input){
			if( $key == 'address_line_2' ){
				$this->view->address_line_2 = $input;
				continue;
			}
			if( $key == 'user_email' ){
				$this->view->from = $input;
				continue;
			}
			if(empty($input)){				
				$errors[] = '<span class="errors">Please enter '.str_replace('_',' ',$key).'</span><br>';
			}
			$this->view->$key = $input;
		}
		if(@$errors){
			$this->view->errors = $errors;
			$this->render('address');
		}else{
			$_POST['date_joined'] = time();
			unset($_POST['Submit_x']);
			unset($_POST['Submit_y']);
			$oZend_Db_Adapter_Pdo_Mysql->insert('Members',$_POST);
			$oAuthNamespace->post['user_id'] = $oZend_Db_Adapter_Pdo_Mysql->lastInsertId();
			header('location:surveys');
			exit;
		}
	}
	$this->view->first_name = '';
	$this->view->last_name = '';
	$this->view->address_line_1 = '';
	$this->view->address_line_2 = '';
	$this->view->city = '';
	$this->view->zip_code = '';
	$this->view->phonearea = '';
	$this->view->phoneprefix = '';
	$this->view->phoneprefix = '';
	$this->view->phoneline = '';
	$this->view->from = @$_GET['from'];
}
}

put the config data into a file and name it something  like 'config.inc', then just add the line at the top of each page: include('config.inc'). there you go.

 

that doesn't work for what Im doing...I want to use the variable assigned in the first file.

Well, a cheat way to use it would be to do as radi8 said, and then just define your $link variable as a global in your methods.

 

However, I recommend you don't do this with classes. Instead, create a class for your database, and create a query method. Then, just query your DB through the method.

 

Have the method require sql or something. Like this:

 

$connection = new DatabaseClass();

/* ... */

$sql = "My SQL code here";

$connection->query( $sql );

There is also the use of $_SYSTEM['host'] et al which makes these variables accessible everywhere in your app for the current session. I would still put the connection info in a separate file and include it on your entry page or right after the log-in page. From there set the $_SYSTEM['<vars>']. As long as the session is valid, the variables are then accessible regardless of where they are being accessed from.

Also, if using this method, the line session_start(); must be declared at the top of every page that will need them.

There is also the use of $_SYSTEM['host'] et al which makes these variables accessible everywhere in your app for the current session. I would still put the connection info in a separate file and include it on your entry page or right after the log-in page. From there set the $_SYSTEM['<vars>']. As long as the session is valid, the variables are then accessible regardless of where they are being accessed from.

Also, if using this method, the line session_start(); must be declared at the top of every page that will need them.

 

OK, my bad... change $_SYSTEM to $_SESSION. I had my head up where the sun ain't shining and got that all pooched up.

i'm not using the zend framework, but it sounds like you may want to consider employing a singleton class that encapsulates a database connection. any class/page/whatever that needs the connection while servicing a request will ask the singleton for it's instance and use the connection.

 

i can post an example of the singleton, if you're interested in this route.

 

jason

i'm not using the zend framework, but it sounds like you may want to consider employing a singleton class that encapsulates a database connection. any class/page/whatever that needs the connection while servicing a request will ask the singleton for it's instance and use the connection.

 

i can post an example of the singleton, if you're interested in this route.

 

jason

 

Yes an example would be great! :)

thanks, but which one is the model and the controller.

 

I set it like this:

first file is the config.php

 

I include config.php into my model which is the second file and I extend AppInfo

 

then I create my controller which already extends Zend_Controller_Action so I can't extend my model here.

 

I get a Fatal error: Call to private AppInfo::__construct() from context 'Zend_Loader' in C:\xampplite\htdocs\app\controllers\IndexController.php on line 6

put the config data into a file and name it something  like 'config.inc', then just add the line at the top of each page: include('config.inc'). there you go.

 

As a side note, never do this. Unless specifically configured to, your webserver will process the .inc file as plain text and send it to the browser, allowing anyone to view your database connection info. It's an easy and stupid way to find yourself "hacked" (I use the term loosely)

thanks, but which one is the model and the controller.

 

I set it like this:

first file is the config.php

 

I include config.php into my model which is the second file and I extend AppInfo

 

then I create my controller which already extends Zend_Controller_Action so I can't extend my model here.

 

I get a Fatal error: Call to private AppInfo::__construct() from context 'Zend_Loader' in C:\xampplite\htdocs\app\controllers\IndexController.php on line 6

 

i wouldn't use inheritance to acquire the functionality, but rather composition.

 

jason

put the config data into a file and name it something  like 'config.inc', then just add the line at the top of each page: include('config.inc'). there you go.

 

As a side note, never do this. Unless specifically configured to, your webserver will process the .inc file as plain text and send it to the browser, allowing anyone to view your database connection info. It's an easy and stupid way to find yourself "hacked" (I use the term loosely)

 

if you put it above the web root!

put the config data into a file and name it something  like 'config.inc', then just add the line at the top of each page: include('config.inc'). there you go.

 

As a side note, never do this. Unless specifically configured to, your webserver will process the .inc file as plain text and send it to the browser, allowing anyone to view your database connection info. It's an easy and stupid way to find yourself "hacked" (I use the term loosely)

 

Maybe you would be better off saying that using the inc extension is a bad idea rather than saying that using the include statement was bad.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.