473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2078
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
1606
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 years. Here is the problem I have run across. I am using SPE w/WXGlade to setup the GUI, so it handled the initial formatting of my python code as two seperate classes, one for the APP and one for the Frame. I'll be the first to admit I haven't...
0
3939
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
0
1627
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: 'NoneType' object has no attribute 'cursor'
15
4591
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 communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
4
3162
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: client.c:147: error: incompatible implicit declaration of function UpdateStatus client.c:37: error: previous implicit declaration of UpdateStatus was here The code is as below:
3
1438
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). However, nothing seems to be working. When I execute the script it does nothing, no error messages, nothing. I know that PHP is working because I've done the phpinfo(); thing and it works. I also know my database is working because I can access it...
221
367332
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
10
3388
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 { public static void main (String args) {
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.