开发者

mysql performance for php calls

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?开发者_JAVA百科

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()

}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜