How should I be dealing with MySQL connection...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kilbad
Forum Commoner
Posts: 28
Joined: Wed Apr 02, 2008 3:51 pm

How should I be dealing with MySQL connection...

Post by kilbad »

I am creating an authentication class in which I check a provided username and password against a database table of usernames and passwords.

My question is, how should I be providing the MySQL connection information to the class for this check? Should I pass all the parameters (i.e. the MySQL login info (user, pass, database) table and table fields)) and then establish a connection and query within the class? Or should I open up a connection outside the class and set that connection to a variable and then pass that to the class? Should I pass the query statement? I just want to keep things flexible and generic, and wasn't sure of the best way to do this.

What would you recommend? Do you have any simple example code you could provide?

Thanks in advance!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How should I be dealing with MySQL connection...

Post by onion2k »

I prefer to open the connection outside of the class and then pass it in by reference. This is because I use ADODB Lite and like to have the connection debugging stuff like timings work even when the connection is reused between several objects.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How should I be dealing with MySQL connection...

Post by pickle »

I have my DB class create the connection itself. My Authentication class then calls a singleton function to import the connection.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply