REAL STUPID php/Mysql mistake !
I guess sometimes you have to learn the hard way.
I have programmed many modules for Pictures2Rate, for about 5 of these I have connected to the database with this code in the in the top of each file -
<?php
$con = mysql_connect(”localhost”,”USERNAME”,”PASSWORD”);
if (!$con)
{ die(’Could not connect: ‘ . mysql_error()); }
mysql_select_db(”DB_NAME”, $con);
//CODE STARTS HERE
?>
The problem with this is that if I change the password for the database, all php files using this, will stop working.
So what happend ? I changed the password and forgot about these files. = BIG PROBLEM.
Last night Pictures2Rate had around 9 hours downtime because of this schoolboy error !
So here’s a fix for this problem (Most will guess this or should know this!) -
Put the code to connect to the DB in a separate file, called DB.php
Then at the begining of each php/mysql file that needs to connect top the database simply include this file.
Example -
<?php
include(”DB.php”);
//REST OF CODE GOES HERE AS NORMAL
?>
What this does is include all the user name and password code stated about in the top of each file, so if you change the password you only need to change the password in the file DB.php .
PicturesRate is now back online. Whoops. Lesson learnt.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

I love include files. They make it so easy to modify your scripts. A better software design would be if you used a single configuration include file where all the variables are set. For example wordpress has a single wp-config.php file where the DB username, password, host and other site specific settings are set. The DB connection code is placed in a separate file.