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'];
}
}

Link to comment
Share on other sites

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 );

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

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.