?
Solved

Database connection in PHP

Posted on 2013-05-10
2
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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