This blog has moved here: woorkup.com | FOLLOW ME ON TWITTER @woork
Saturday, October 6, 2007

Config.php: define your database connection's parameters

In this lesson we will create a new MySQL database using phpMyAdmin and a config.php file in the site root with connection's parameters for the database (database name, database host, username, password).



Open config.php and copy and paste the code replacing the value of variables $db_host, $db_name, $username, $password, with correct parameters.

<?php
// Connection's Parameters
$db_host="localhost";
$db_name="database_name";
$username="database_username";
$password="database_password";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>


Now, in the site root, create index.php file and use PHP include() function to include config.php into the page.

<?php include('config.php') ?>




This is a simple way to enstablish a connection with your database MySQL to be used when you need to execute a SQL query.
The index.php structure will be explained in the next lessons.

blog comments powered by Disqus
Anonymous said...

Thanks for this post!

Mark said...

very helpful
Can this use php mail() function?

Christian said...

Why do you connect and select the database twice? The first time is enough :)

Arun Reginald said...

I suggest include_once("config.php"); be used for the includes, as it includes the configurations only once.

  • Twitter Follow woork on Twitter
  • RSS Feed Subscribe to stay up to date
  • Podcast Coming soon...
  • 0 delicious lovers save
Share your links. Do you want to suggest any interesting link about web design or tech news? Submit your link.
Submit a News