Solved

Database connection in PHP

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

695 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