efficacious Posted March 17, 2010 Share Posted March 17, 2010 Hello everyone, I'm writing a MySql DB object for my application and I currently have my db credentials written as protected in the class file. What I want to know is how can I auto populate those variables with information gathered from a user. This would be for the install process. Any help is appreciated thanks all. I Tried this but its throwing an error when it comes across the proteced variables. <?php class MySqlDB { function __construct() { include_once('config/config.php'); } } ?> The config file looks like this: <?php protected $DbName = 'MyDatabase'; protected $DbUser = 'MyUser'; protected $DbPass = 'MyPass'; protected $DbHost = 'localhost'; ?> Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/ Share on other sites More sharing options...
efficacious Posted March 18, 2010 Author Share Posted March 18, 2010 I finally got it working.. It took some thought but I think this is a good solution.. Any1 who knows better please feel free to correct me.. The Config file "config.php" <?php $DbName = 'TheDbName'; $DbUser = 'TheUser'; $DbPass = 'ThePassword'; $DbHost = 'localhost'; $DbInfo = array($DbName,$DbUser,$DbPass,$DbHost); ?> The DB class: <?php class MySqlDB { protected $DbName = ''; protected $DbUser = ''; protected $DbPass = ''; protected $DbHost = ''; //Accepts db information in an array upon object creation. function __construct($DbInfoArray) { $this->DbName = $DbInfoArray[0]; $this->DbUser = $DbInfoArray[1]; $this->DbPass = $DbInfoArray[2]; $this->DbHost = $DbInfoArray[3]; } } ?> Then in my global include file I start the session and import the data array from the config file. Then so that its always available when needed I serialize the array and store in a session variable. <?php //Start the session session_start(); //Include and serialize DB Credz require_once('config.php'); if(!(isset($_SESSION['P_DbInfo']))) { $_SESSION['P_DbInfo'] = serialize($DbInfo); } ?> When I need to create a DB object I simply unserialize the data and pass it into the object. <?php //Retrieve DB info from session data $U_DbInfo = unserialize($_SESSION['P_DbInfo']); //Plug data array into MySqlDB Object $db = new MySqlDB($U_DbInfo); ?> Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027936 Share on other sites More sharing options...
efficacious Posted March 18, 2010 Author Share Posted March 18, 2010 Are there any thoughts on this? I'm just trying to figure out if this is an acceptable solution as far as good coding practices. I mean is the data pretty secure stored in the session variable like that? I assume so since most of the user objects i've come across store user information that way. Thanks for your help all. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027940 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 You could do without serializing / un-serializing. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027941 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 There's really no need for the data to be stored within a users $_SESSION either considering its not specific to a user. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027942 Share on other sites More sharing options...
efficacious Posted March 18, 2010 Author Share Posted March 18, 2010 Very good point.. how else can i achieve this? Btw I'm building this for learning purposes.. my own not for school or anything like that. I just like to do it. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027943 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 Not sure what you mean. You simply don't need to serialize the data, nor store it within the $_SESSION array. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027944 Share on other sites More sharing options...
efficacious Posted March 18, 2010 Author Share Posted March 18, 2010 i mean how can i get the db information into the db object. Obviously I could simply hardcode them directly into the class but if I was to give or sell the final product to somone I couldn't very well have them editing system files. The idea is to present a form during installation that takes and saves the db information for use. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027946 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 require_once('config.php'); $db = new MySqlDB($DbInfo); Any install script would just need to gather the users data and create the config.php file. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027948 Share on other sites More sharing options...
efficacious Posted March 18, 2010 Author Share Posted March 18, 2010 Doh.. Thanks Thorpe, I just need to cut the fat. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027949 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 We can all over complicate things at times. Link to comment https://forums.phpfreaks.com/topic/195617-auto-populating-db-variables-to-db-class-need-help/#findComment-1027951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.