ballhogjoni Posted April 23, 2009 Share Posted April 23, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/ Share on other sites More sharing options...
radi8 Posted April 23, 2009 Share Posted April 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817031 Share on other sites More sharing options...
ballhogjoni Posted April 23, 2009 Author Share Posted April 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817056 Share on other sites More sharing options...
alphanumetrix Posted April 23, 2009 Share Posted April 23, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817062 Share on other sites More sharing options...
radi8 Posted April 23, 2009 Share Posted April 23, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817582 Share on other sites More sharing options...
radi8 Posted April 23, 2009 Share Posted April 23, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817623 Share on other sites More sharing options...
redarrow Posted April 23, 2009 Share Posted April 23, 2009 Why not just create a database class, and add it , to the pages that need it? Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817627 Share on other sites More sharing options...
ballhogjoni Posted April 23, 2009 Author Share Posted April 23, 2009 Why not just create a database class, and add it , to the pages that need it? can you give me an example...Im using the Zend framework and I dont think it works that. Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817921 Share on other sites More sharing options...
nankoweap Posted April 23, 2009 Share Posted April 23, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-817933 Share on other sites More sharing options...
ballhogjoni Posted April 28, 2009 Author Share Posted April 28, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-820837 Share on other sites More sharing options...
nankoweap Posted April 28, 2009 Share Posted April 28, 2009 check out my post here: http://www.phpfreaks.com/forums/index.php/topic,249790.0.html Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-820839 Share on other sites More sharing options...
ballhogjoni Posted April 28, 2009 Author Share Posted April 28, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-820858 Share on other sites More sharing options...
trq Posted April 28, 2009 Share Posted April 28, 2009 Take a look at Zend_Registry. Link to comment https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-820972 Share on other sites More sharing options...
Prismatic Posted April 28, 2009 Share Posted April 28, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-820974 Share on other sites More sharing options...
nankoweap Posted April 28, 2009 Share Posted April 28, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-821009 Share on other sites More sharing options...
ballhogjoni Posted April 28, 2009 Author Share Posted April 28, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-821456 Share on other sites More sharing options...
radi8 Posted April 29, 2009 Share Posted April 29, 2009 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 https://forums.phpfreaks.com/topic/155293-how-to-make-db-config-connection-available-throughout-entire-app/#findComment-822023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.