is this bad practise?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

is this bad practise?

Post by konstandinos »

hi i have some standard php functions, that pull data from a database and spit out formatted html.

so first i have the base script:

Code: Select all

<?php
     include("functions/foo.php");
     print_html_header();
     fubar($table);
     print_html_footer();     
?>
then the fubar() function, described below, initialises the mysql connection, queries the dbase for the data, and outputs the formatted html...

Code: Select all

// inside foo.php
...

     function fubar($table)
     {
           $connection_A = mysql_connect(...);

           //some code...

           another_function_call();

           mysql_close($connection_A);
     }
...
but then i have another function, another_function_call(), which also inits a sql connection to the same database...

Code: Select all

// further down in foo.php
...
     function another_function_call()
     {
          $connection_B = mysql_connect(...same database);
          
          //do stuff
     
          mysql_close($connection_B);
     }
...
what i am wondering is if this is bad? it's almost like a nested mysql connection, for use of a better description.

i personally don't see anything wrong with it. but then again, if ive already initialised a sql connection, should i try use the same one? or is there no danger in my current approach?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

From http://uk.php.net/mysql_connect
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
Personally i wouldn't call mysql_connect(...) once more in this case.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Moving this to PHP Theory & Design.
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

kaszu wrote:...
Personally i wouldn't call mysql_connect(...) once more in this case.
yeah i guess you're right. i can just have mysql_query(...) inside of another_function_call() and it will use the current connection.

ok thanks for clearnig that up.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

you can normally have a database manager to handle database connections.
you can use singleton design pattern to reuse database connections.
but do not the same connection if you are handling transactions.
you can same connection for simple searches and normal retrieval of data from DB but do not use connection if you are dealing with creating orders and processing payments.

if you do not want to handle complex OO practices, you can still pass the same DB connection to various functions that are called in a page but it would get messy when the application grows large.
Post Reply