Jump to content

auto populating db variables to db class need help


efficacious

Recommended Posts

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

?>

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

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.

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.

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.