For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > April 2004 > placement mysql connections - coding style









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author placement mysql connections - coding style
cnaught

2004-04-05, 12:34 pm

I have a question about the use of mysql connections within php code.

I use include files as libraries, which each contain many functions,
most of the accessing my mysql database to perform a specific
function.

Currently I have it setup so that within each function that calls the
database, at the top of that function, I include a file with my
database connection variables, and then I open the mysql connection,
perform any necesssary database queries, then close that connection.

These functions are shared throughout my code and used from a variety
of purposes.

Example:

///////////////////////////////////////
function get_email_addr($user_id)
{
// include database connection variables
include('db_info.php');

// make connection to database
mysql_connect($db_hostname, $db_username, $db_password) or die
("unable to connect to database");

// select database
@mysql_select_db("$db_name") or die ("unable to select database");

// build sql string to get email address
$sql = "select ";
$sql .= " email_addr ";
$sql .= "from ";
$sql .= " user ";
$sql .= "where ";
$sql .= " user_id = ".$user_id." ";

// execute sql string
$sql_result = mysql_query($sql) or die (mysql_error());

$row = mysql_fetch_array($sql_result);
$user_email_addr = $row['email_addr'];

// close database connection
mysql_close($db_conn);

return $user_email_addr;

}
///////////////////////////////////////

I am wondering if this is a good way to set it up. Because the
problem I am finding now is that when I am calling a function like
this, from another function that needs has an open db connection, then
call the above function, then make another db call from the parent
function, the the above function will close the db connection I am
using in the parent function, and I have to reopen the connection
again.

What do you guys think is the best way to setup my connections so I
don't have this problem. Because it is starting to become pain in the
butt. For example if I were to call the above function sometimes from
a parent function (So if i didn't call it, I don't have to reopen a
connection, if I do call it, I do).

Any suggestions examples of coding style to best address this issue?

Thanks
Jason
Kreso

2004-04-07, 10:35 am

cnaught wrote:
> I have a question about the use of mysql connections within php code.
>
> I use include files as libraries, which each contain many functions,
> most of the accessing my mysql database to perform a specific
> function.
>
> Currently I have it setup so that within each function that calls the
> database, at the top of that function, I include a file with my
> database connection variables, and then I open the mysql connection,
> perform any necesssary database queries, then close that connection.
>
> These functions are shared throughout my code and used from a variety
> of purposes.
>
> Example:
>
> ///////////////////////////////////////
> function get_email_addr($user_id)
> {
> // include database connection variables
> include('db_info.php');
>
> // make connection to database
> mysql_connect($db_hostname, $db_username, $db_password) or die
> ("unable to connect to database");
>
> // select database
> @mysql_select_db("$db_name") or die ("unable to select database");
>
> // build sql string to get email address
> $sql = "select ";
> $sql .= " email_addr ";
> $sql .= "from ";
> $sql .= " user ";
> $sql .= "where ";
> $sql .= " user_id = ".$user_id." ";
>
> // execute sql string
> $sql_result = mysql_query($sql) or die (mysql_error());
>
> $row = mysql_fetch_array($sql_result);
> $user_email_addr = $row['email_addr'];
>
> // close database connection
> mysql_close($db_conn);
>
> return $user_email_addr;
>
> }
> ///////////////////////////////////////
>
> I am wondering if this is a good way to set it up. Because the
> problem I am finding now is that when I am calling a function like
> this, from another function that needs has an open db connection, then
> call the above function, then make another db call from the parent
> function, the the above function will close the db connection I am
> using in the parent function, and I have to reopen the connection
> again.
>
> What do you guys think is the best way to setup my connections so I
> don't have this problem. Because it is starting to become pain in the
> butt. For example if I were to call the above function sometimes from
> a parent function (So if i didn't call it, I don't have to reopen a
> connection, if I do call it, I do).
>

in main.php file do check if action you are supposed to do is using any of
functions that are in include.file . if is so, include it just
once in main.php


--
kreso
http://www.plus.hr/cgi-bin/aff/g.o/gdprom
www.gdprom.com


cnaught

2004-04-19, 4:41 pm

Thanks for the input Kesco, but I don't think that you understood my
question correctly. I do include my include.files when I need to call
the functions within them.

My question was: How can I improve the way I have the mysql database
connections setup. Having them in a function scope seems to cause
problems. Because, if I am in function A using a database connection,
then I call function B that opens and closes a database connection,
and then the code in function A, following the call function B, no
longer has a database connection, its closed, because the scope within
function A closed it.

cnaught

"Kreso" <kreso@purger.com> wrote in message news:<c5108n$2n4chc$1@ID-208070.news.uni-berlin.de>...
> cnaught wrote:
> in main.php file do check if action you are supposed to do is using any of
> functions that are in include.file . if is so, include it just
> once in main.php

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com