472,799 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 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 2039
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.