WeCodeThings Library
Library of code bits
Glossary    Contact Us
Need a web designer? visit WeCodeThings.Com for more details
Search  
   
Browse by Category

WeCodeThings Library .: PHP .: PHP Simple MySQL Open Configure and Close the Connection to a Database using PHP



PHP Simple MySQL Open Configure and Close the Connection to a Database using PHP

Usually you want to create a “config.php” file, a “opendb.php” file, and a “closedb.php” file.

In the config.php file:

<?php
// config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'databaseName';
?>

In the opendb.php file:

<?php
// opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

In the closedb.php file:

<?php
// closedb.php
// not needed, but can help
mysql_close($conn);
?>

Now, we include all 3 files like this:

<?php
include 'config.php';
include 'opendb.php';

// insert, update select, or delete statement goes here

include 'closedb.php';
?>



How helpful was this article to you?

Related Articles

article PHP Simple Insert Into MySQL Database using isset function $_POST
To insert an item into the database, you...

(No rating)  5-8-2009    Views: 1413   
article mySQL SQL Select the last 50 rows inserted into the database by using LIMIT
In mySQL you can show a specific number of...

(No rating)  8-3-2009    Views: 686   
article SQL Sum a Sum MS SQL Server, Oracle, MySQL Sum a Group By
SQL SUM A SUM For some reason, this was...

(No rating)  11-24-2009    Views: 836   

User Comments

Add Comment
No comments have been posted.




.: Powered by Lore .:Code by WeCodeThings Website Design and Development