Page 1 of 1
Simple Question
Posted: Fri Sep 01, 2006 8:33 am
by s.dot
Albeit I always think my questions are simple, they always tend to get drug out into long discussions. =/ Anyhoot, is it "acceptable" to hardcode a username and password into a database class? So I could just call a connect function. Perhaps something like this:
Code: Select all
class db
{
var $host = 'localhost';
var $user = 'username';
var $pass = 'password';
function connect()
{
mysql_connect($this->host,$this->user,$this->pass);
}
}
So on a script I can just call
Code: Select all
require 'db.class.php';
$db = new db();
$db->connect();
Forgive me if this is how it's "usually" done, as I don't much look at others code. To me this seems logical because this is for one project that only I will ever see the code to. However, I don't want to start a bad practice.
Posted: Fri Sep 01, 2006 8:51 am
by malcolmboston
This is also how i personally do it as i have never had the need arise where i would need to connect as a different user
perfectly acceptable in my opinion
Re: Simple Question
Posted: Fri Sep 01, 2006 8:57 am
by Oren
If you do it like that, then why not doing it like this?
Code: Select all
class db
{
function connect()
{
mysql_connect('localhost', 'username', 'password');
}
}
Posted: Fri Sep 01, 2006 9:00 am
by malcolmboston
i do it scottay's way purely because i prefer the seperation of potentially 'changing' configuration options, that way i do not need to even touch the methods, only the classes configuration vars
Re: Simple Question
Posted: Fri Sep 01, 2006 9:03 am
by s.dot
Oren wrote:If you do it like that, then why not doing it like this?
Code: Select all
class db
{
function connect()
{
mysql_connect('localhost', 'username', 'password');
}
}
Also, should the need arise to perhaps have a certain script connect to a different db (never happened to me before, just something that MIGHT occur), I could change the settings by doing:
Code: Select all
$db = new db();
$db->user = 'some other user';
$db->pass = 'xxxxxx';
Right?
Posted: Fri Sep 01, 2006 9:08 am
by Jenk
None of my details are ever kept within the class. Simply because I use the same class on many applications.
I use my machine and my database for developing applications for clients, with my classes, so the login details will differ.
All configurable items are kept in a configurations file or medium (table for example).
Ease of maintenance..
Posted: Fri Sep 01, 2006 9:13 am
by Todd_Z
I have a mySQL wrapper class, which i use basically three methods from it:
MySQL::getAll( $sql );
MySQL::execSQL( $sql );
MySQL::nextObject();
The execSQL method is the only place in the entire code with the mysql_query function being used. Here I check for an open mysql resource, and if its not available, then I call a static method in that class to connect. This way, you never have to actually connect to the database explicitly, it handles it for you.
Posted: Fri Sep 01, 2006 9:32 am
by s.dot
Jenk wrote:None of my details are ever kept within the class. Simply because I use the same class on many applications.
I use my machine and my database for developing applications for clients, with my classes, so the login details will differ.
All configurable items are kept in a configurations file or medium (table for example).
Ease of maintenance..
But.. all you would have to do is simply change the login credentials in the class for each project. Seems like storing them somewhere else would take away from the ease of maitenance?
Sorry, I'm just a bit confused
Also, it seems like while I'm writing this I'm just giving different names to the php functions. The reason I wanted to write this is to cut down on always typing out queries and other db functions the long way, to save some typing.. and to experience dealing with objects.
So I suppose it is a "wrapper" if that's what it's called. Can someone tell me the purpose of such a script, other than to cut down on coding?
Perhaps it is to get a solid class and reduce the chance of coding errors? To have something to transport from project to project?
Seems to me if I start getting used to a class that I write (such as this db class), I will start forgetting the real php functions!
Here's what I got so far:
Code: Select all
<?php
//temp
ini_set("display_errors","On");
error_reporting(E_ALL);
/*
* MySQL Database Class
*/
class db
{
var $host = 'localhost';
var $user = 'user';
var $pass = 'pass';
var $db_name = 'db_name';
var $resource;
//connect to mysql server
function connect()
{
if($this->resource = mysql_connect($this->host,$this->user,$this->pass))
{
return true;
} else
{
return false;
}
}
//select database
function select_db()
{
return mysql_select_db($this->db_name,$this->resource);
}
//query the database
function query($sql)
{
return mysql_query($sql);
}
//fetch array from result set, defaults to MYSQL_ASSOC
function fetch($result,$type=MYSQL_ASSOC)
{
return mysql_fetch_array($result,$type);
}
//frees result set from mysql memory
function free_result($result)
{
mysql_free_result($result);
}
}
?>
[edit] I suppose I went from a simple question, to... something else, didn't I?

Posted: Fri Sep 01, 2006 9:37 am
by Jenk
Do you use a framework with a service locator?
Do you remember to open up your mysql class file
everytime you create a new application?
Ease of maintenance:
Code: Select all
<?php
$db = new MySQLClass('host', 'user', 'pass');
?>
Not ease of maintenance:
Open folders > Open Class file > Edit login details >
Then there is also the situation of what if the client changes database name/password? They don't want to have to manually edit the source files, that's why the paid you in the first place. They just want an easy to use config handler such as a webpage or even an ini file.
Posted: Fri Sep 01, 2006 9:41 am
by alex.barylski
Well if it works...it's acceptable
By personal choice...I wouldn't do that...
It's bad practice and poor use of OOP IMHO...
You'd be better passing an Ado object (initialized somewhere else) to your class and using the object instead of calling mysql_* directly or hard coding user/pass details inside a class...
Just a personal choice though...either is perfectly technically valid, just the latter is better suited for project longevity...

Posted: Fri Sep 01, 2006 9:50 am
by s.dot
Heh, but then it would be a choice of whether to edit the class, or edit the object instantiation. Either way would still need editing. Even a webpage or ini file... still editing. I get the point though. It's pretty much a matter of preference.
What about this?
scottayy wrote:So I suppose it is a "wrapper" if that's what it's called. Can someone tell me the purpose of such a script, other than to cut down on coding?
Perhaps it is to get a solid class and reduce the chance of coding errors? To have something to transport from project to project?
Seems to me if I start getting used to a class that I write (such as this db class), I will start forgetting the real php functions!
Posted: Fri Sep 01, 2006 10:12 am
by Jenk
But with a webpage/configfile/table you'd only need to edit the one thing when installing the app or when the system changes etc, over potentially editing every class file that has properties set like that

Posted: Sat Sep 02, 2006 4:05 am
by BDKR
What's wrong with some constant or something set in a config file?
IMHO, you shouldn't have to recode a lib everytime you start a project or change database machines.