in the past I have used smt like:
$con = mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("xxx");
But now, I’ve noticed everybody started using smt like this:
if (!defined('DB_HOST')) define('DB_HOST','localhost');
if (!defined('DB_USER')) define('DB_USER','root');
if (!defined('DB_PASS')) define('DB_PASS','root');
if (!defined('DB_NAME')) define('DB_NAME','xxx');
What’s different about their usage? For example echoing, filtering..
Thanks for help
Well, none of them are wrong, it’s your choice. I think the second alternative is more like a good practice. But my statement is valid only if you define it in one file and
require_oncein the others. Example:db_config.php:query.php:But why? Imagine that you want to change your database name, user or password. If you have code
mysql_connect("localhost", "root", "root")every time you needed it, a change of plans will make you go through a lot of re-coding. But if you define them in one place as the second alternative does, you won’t have to rewrite a lot of files.