Hi all.. pretty darn new to php and I've been slapping up lots of different mysql queries on my home page. It's all working great, but... I'm obviously concerned with how I'm handling the connections, because I don't really know the proper way. For example, I have 2 separate queries at the top of the page, displaying 2 different items on the page. Further down... I'm doing a loop and populating 10 rows ... and then 1 more query after that on the bottom of the page. Could someone advise on when/if I should be closing the connections... or when the appropriate time would normally be to do this? Thanks!
Here is how i do it: <?php $dbhost = "DATABASE_HOST"; $dbname = "DATABASE_NAME"; $dbuser = "DATABASE_USERNAME"; $dbpass = "DATABASE_PASSWORD"; if($dbhost == ""){ echo "<font color='red'>Ooops, there is no host.</font>"; $error = true; exit(); } else if($dbname == ""){ echo "<font color='red'>Ooops, there is no database name.</font>"; $error = true; exit(); } else if($dbuser == ""){ echo "<font color='red'>Ooops, there is no database username.</font>"; $error = true; exit(); } else if($dbpass == ""){ echo "<font color='red'>Ooops, there is no database password.</font>"; $error = true; exit(); } if($error == false){ mysql_connect($dbhost, $dbuser, $dbpass) or die("<font color='red'>Ooops, there is an error. It could be; host, username or password.</font>"); mysql_select_db($dbname) or die("<font color='red'>Ooops, there is a problem with the database.</font>"); } ?> PHP: Just play around with it a little. Learning programming is Trial and Error. If you get error's just work around fixing it, i had lots of errors when i first started. Just search what the error is on google. Have fun.
Also, i would have the connect file in a seperate page. All you then have to do is include it: <?php include("connect.php"); ?> PHP: Or you could require it: <?php require("connect.php"); ?> PHP: Any problems, just ask me.
you dont need to close the connection,it will be released by itself, If you need to select data from different table, there is no good way. If you site has a great traffic, you can open your config file , set $pconnect = 1
Thanks! That was what I was looking for. As stated above.. I'm connecting to the db fine and pulling all the information I need. I just wasn't sure if I was leaving open connections out there that could potentially cause problems. Appreciate all the advice from all.