Performance of connections to mysql

Codes here !

Moderators: egami, macek, gesf

Post Reply
victor123
New php-forum User
New php-forum User
Posts: 192
Joined: Mon Sep 06, 2004 1:23 am
Location: Madrid, Spain

Mon Sep 06, 2004 1:30 am

Hi,

I am designing an application (php+mysql) that has a lot of queries to the db. I would like to know what is the best alternative:

- Have a @mysql_connect at every page the application has. In this case, should the connection be closed when abandoning the page?

- Have a single @mysql_connect.

Many thanks in advance.

Regards, Victor

User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Mon Sep 06, 2004 7:12 am

Nop, there's no need for so such connections!
What i recommend is to write your DB connection code in a separated file and call it at very top of your files using require_once(), so the connection will be available even for different php code blocks in a file!
So... you will just need to close the Db connection once and at the bottom of your file!

Example:

Code: Select all

<?php

    // including your Db connection file!
    require_once 'conn.php';

    mysql_query("some query");
?>

    Something in between

<?php
    mysql_query("another query");
?>

    Something in between

<?php
    mysql_close();
?>

You better do this if you're using the header/footer include technic!

Cheers
Sincerely,
Gonçalo "gesf" Fontoura

User avatar
mike
New php-forum User
New php-forum User
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens
Contact:

Tue Sep 07, 2004 1:25 am

I agree with that. Include the file, with a single connection in it, in your scripts. No need to use mysql_close() at the end
11:16am up 404 days, 2 users, load average: 148.68, 143.45, 132.6


Mike Vassileiou
[Systems Administrator / PHP Developer]


PHP/MySQL Manual:
http://www.phpmysql-manual.com

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1279
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Tue Sep 07, 2004 9:02 pm

If you have few users on the site and making many connections... all the time..
I recommend you mysql_pconnect
But be sure you have few users .. say less than 30 because pconnect leaves the connection open all the time even after the page has finished executing

Post Reply