Solved

Database connection in PHP

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now