Solved

Database connection in PHP

Posted on 2013-05-10
2
269 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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