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

?>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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