Solved

Database connection in PHP

Posted on 2013-05-10
2
267 Views
Last Modified: 2013-05-10
Dear Experts,

I'm currently trying to improve a website and the next logical step would be to store data in a general place. Namely this will be necessary for the news as they are currently hard-coded at a few different files.

So I tried my luck with a MySQL Server (currently localhost + XAMPP), while it works just as expected I still have a few questions open how to do it properly and in general about db-connections in php:

Is it possible to have more than one db-connection open at the same time?

What's the general consensus about how to handle db-functions?

I currently would do something like this:

function open_db($p1,$p2,...){
}
function close_db($p1,$p2,...){
}

function read_data($p1 <- SQL-Statement){
open_db(x,y);
mysql_db_query($p1);
close_db(x,y);

}

Open in new window



Or is it better if I build it like a class?
0
Comment
Question by:goldwynexe
2 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
ID: 39155154
Well in real life you can have many connections open at the same time. If 100000 users log onto your website you will have 100000 connections. I prefer to always use a class when doing database operations. It is cleaner and re-useable code.

You write one class that has the main functions you need to use. A typical set of functions would be

public function __construct() {
// connect to your host here and handle any errors
// if no errors call the open function
}

public function open() {
   mysql_select_db('your_database_name');

}

public function query($query) {
   // here you could send in the actual query string or what I like to do is
send in an associated array of key (column names) and values.
I usually create a list of acceptable keys for each table and loop through those keys. If any keys are in the data array that are not in the acceptable array then I unset them before running the query.

Also, you can do a str_replace on each key and each value to make sure the keys have back tics `  and values have single quotes around them. This will make sure your query doesn't break.
}

Another useful class function would be

public function data($data = false) {

}

The idea here is to send in an array of keys that you want the values for. If you send in no data array then go ahead and do a select * from the table. Other wize only select those keys that are in the data array.


I write an api class for each table. Inside that class I include the database class and instantiate it. Also in each class I make a global variable $this->table = 'table_name'; so when I call my query like this:

$result = $this->db->query("SELECT * FROM `".$this->table."`"); // etc....


Finally, you don't have to close a database connection with php - it will close when the script dies (finishes executing) but a lot of people do close them. Either way it makes no difference.

I hope this helps some.
0
 

Author Closing Comment

by:goldwynexe
ID: 39155164
Yes this sure helps a lot:)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question