closing database connection calls to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CincoPistolero
Forum Newbie
Posts: 6
Joined: Thu Jun 23, 2005 4:30 pm

closing database connection calls to mysql

Post by CincoPistolero »

I took over the management of php site running mysql. The site is currently getting "reached maximum connections errors" The original programmer said that he didn't close out any of his sql calls. So I tried the below code in a footer. Is this correct.

<?php mysql_close();?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

In the future please do not post your question in multiple forums. I've deleted your post in Databases.

And yes, that will close a mysql connection. See below for more info.

http://php.net/mysql_close
CincoPistolero
Forum Newbie
Posts: 6
Joined: Thu Jun 23, 2005 4:30 pm

Post by CincoPistolero »

I manange a site that has been working fine since I took over a few weeks ago. I've only made text changes, and at 10am this morning the site gets the following error:

WARNING: mysql_connect(); User ***** has already more than m'max_user_connections' active connections in ../../../database.php line 6
Unable to connect.

The guy who originally created the site never coded <?php mysql_close(); ?> but it is my understanding that closing isn't necessarily needed. I added the closing statement to the footer file and uploaded it, but I still get the above error. Any suggestions? :!:
CincoPistolero
Forum Newbie
Posts: 6
Joined: Thu Jun 23, 2005 4:30 pm

Post by CincoPistolero »

Sorry about the database post, I wasn't sure which forum it belonged in, and thanks. I figured it out. The mysql_connection was set in a variable so I had to put that in my close string.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Glad you figured it out. Don't worry about the multiple post, as long as it doesn't happen again we're cool 8)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

From his yet another double post
if i have four select statements on a php page, do I need to close the sql connection for each of those calls or can I just close it once at the end of the page.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Close your connection at the end of your page, and mysql_free_result() after your done with the results, instead of keeping it in memory.
Post Reply