note 89744 deleted from function.mysql-close by danbrown
| From: | danbrown@php.net | Date: | Fri, 20 Mar 2009 23:23:10 +0000 |
| Subject: | note 89744 deleted from function.mysql-close by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152243@lists.php.net to get a copy of this message | ||
Note Submitter: fireworking at gmail dot com
----
I disagree with Jonbarnett that mysql_close is unuseful.
I have been on a tight budget recently. I have been using a free web host and so mysql_close is very
important. It always gives an error: mysql_max_connections because I don't close my connection
until the end. Although you said that it would slow down transferring to the client, it will still
allow your script to run. If I use mysql_close, my down time will be reduced by over 20%. Currently
my website has a 80% dowtime and I hope to change that to 95% with the newer mysqli functions and
mysqli_close.
Also, If you are trying to make scripts to work on PHP4, be prepared for the future. You should just
start at 5 or even 6 if your web host supports it which I doubt unless you are running on a
dedicated server. Php has a new extension called mysqli functions which is better than the mysql
functions.
So Tip: If you are struggling with a lot of down time with mysql connections, you should probably
switch hosts or use mysql close.
You should try an follow this structure:
<?php
//Do some validation
$article_name = stripslashes($_GET['article']0);
//Database variables
define(db_host,'localhost');
define(db_username,'username');
define(db_password,'password');
//Connect to the database
$link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
//Try to minimize time you are in the mysql connection. Although this is slower, the time in mysql
less. (Especially useful if your host restricts max connections)
//Only do things that you need to do within a mysql connection.
$article_name = mysql_escape_string($article_name);
//Get some random results
$result = mysql_query("SELECT * FROM sometable WHERE id='$article_name;");
//Close the connection before doing anything else if you are not going to run any more queries
mysql_close($link);
//Start to do things after you close the link
$row = mysql_fetch_row($result);
echo $row[0];
?>