473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

MySQL connection, when to close, when to connect...

Hi,

I am trying to tidy up my code and now i am looking at my database work.
Now correct me if i am wrong, (I am using mysql_connect).

Lets assume a few functions...

//////////////////////////////////
// code
/////////////////////////////////
function A()
{
// connect
// run a SELECT query
// close
}

function B()
{
// connect
// run a SELECT query
}

Now according to the documentation A() and B() are the same because the
database is closed at the end of the function.
So in function B() the close in implied. Right?

But now lets assume within another function that i do many selects

function testDB()
{
B();
B();
B();
B();
// close the id
}
now surely connecting and closing all the time is very bad for the
performances.

So should i save the connection ID to make sure that the DB only connects if
it is not open?
Something like...

///////////////////
// code
//////////////////

$link = 0;
function A()
{
global $link;
if( $link == 0){
$link = // connect
}
// run a SELECT query
}

The case above would be OK if the function does not close the connection
when it exits?
Should i rather do

// some php
$link = // connect;
function A()
{
global $link;
// if not connected then big problem...
// run a SELECT query
}

function CloseA()
{
// close $link;
}

What would be the best way to do it?
Many thanks

Sims
Jul 17 '05 #1
2 2060
Seb
Hi,

According to me, the connexion is not closed at the end of the function,
but at the end of the script.

I'm using Oracle, but I don't think it's very different.

I've built an abstraction class to manage the database connections and
queries.

I use one method to open the connection and instantiate my objects (that
is my connexions), then other methods to perform queries.

That gives :

Connect = new DB();
Connect->select(....);

With Oracle we also have persistant connections using pconnect : PHP
manage database connexions and don't close them, they are re-used when
needed. I don't know if it exists with MySQL.

Of course opening a new connection for each request in the same script
is really not good ;-). I wouldn't be your database server when traffic
will increase on your site ;-)

Seb
Jul 17 '05 #2
MySQL does support persitent connections.

I have an abstraction layer written myself as well (to support multiple
databases). I generally pass another variable indicating if a persistent
connection is needed. That way, you can choose (when you create the object)
to have persistent connections in your script or not.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josh | last post by:
Hi, First off, I am a newbie to Python and so far I must say that we're getting along pretty well. My schooling was is C/C++, but have been writing code in, dare I say it, VB, for the past 4...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
0
by: Daniel Crespo | last post by:
Hi to all, I'm using adodb for accessing mysql and postgres. My problem relies on the mysql access. Sometimes, when I try to execute a query (using ExecTrans method below), I get this error:...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
4
by: whitemoss | last post by:
Hi, I've made some changes to my coding..but unfortunately, there were errors when compiling it..dunno how to solve it..hope anyone can help me...the errors: client.c: In function senddata:...
3
by: yes_its_just_me | last post by:
Hi everyone, I haven't used PHP since version 4 and am trying to use it for a new project. All I'm trying to do is connect to a MySQL database and show the contents of that database (as a start)....
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
10
rhitam30111985
by: rhitam30111985 | last post by:
hi all .. java/jdbc newbie here ... i cant seem to perform the most basic step for database connectivity using java . here is my code : import java.sql.*; public class connect { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.