views:

41

answers:

3

I have multiple php functions for my code and each function tries to setup mysql connection, send queries and closes mysql connection. Is this a good design or should I set up one connection from a master function and then pass the variables to each of these functions to execute queries?

Also, is it possible that this code will give errors when trying to execute the 2nd instance of mysql_query in func1() ? Won't the instance of mysql connection called in func2 be independent from the instance called in func1? In my code, I seem to get inconsistent behaviors. At times, I am getting error but when I refresh the page, there seems to be no problem. Any suggestions is welcome. Thanks.

def func1() {

mysql_connect(params)

mysql_query()

func2()

mysql_query()

mysql_close()

}


def func2() {

mysql_connect(params)

mysql_query()

mysql_close()

}
+2  A: 

I can't say it's awful performance hit, but it just has no sense.
Database connection intended to be opened once.
If you need to get 5 apples from the box - do you open and close this box 5 times? Or just open it and get all at once? ;-)
Same here.

Just open connection once and never close it. It will be done automatically.

Col. Shrapnel
I agree with you, I was just worrying too much about keeping my code modular and passing less variables. Seems unreasonable though, I can pass another variable certainly.
@user429113 you can keep it modular as well. there are 2 ways. a simple one (I personally prefer it): do not pass anything. All mysql functions can use last opened connection by default. No need to open or pass anything. For 99,9% of your scripts that's enough. The only case when you need to set up connection resource explicitly, is when you need 2 different connections. That's second option you have. store connection resource in a variable and pass it (internally, of course, inside of your database class, no need to pass it when functions being called anywhere else)
Col. Shrapnel
+1  A: 

You should use a singleton to create or get an unique MySQL connection, multiple are not recommended as you have to "name" each connection and specify which you want to use.

MatTheCat
+1  A: 

I usually use one single connection in my projects.

//on the beginning of the file I connect to the database
$bdconn = mysql_connect(/* params */);

    //things to do on the page
    {...}
//at the end of the page I disconnect from it
mysql_close($dbconn);

If you have to connect to other databases as well on run, then you just have to connect to the other one, but then just reconnect to the first one. I think it's not useful to work with more databases in the same time.

Ervin
Removed `@` from your code as it's useless and bad practice
Col. Shrapnel
I'd also strongly recommend that you defer the creation of the connection until your code actually needs to use it if you are concerned about performance. (this is trivial if you wrap the mysql_query calls and use a static variable for the connection)
symcbean