Connect To MySQL Database
Connect To MySQL Database
Shortcuts
Home
RSS feed for page
Tags
Authentication
Date
DateTime
Getting Started
MySQL Installation
PHP
PHP Installation
Validation
View more
Table of Contents
admin
on 12-18-2008
10 people found this article useful.
value:
[Edit Tags]
Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() function like
this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'petstore';
mysql_select_db($dbname);
?>
$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL
server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid
MySQL user name and password. For adding a user to MySQL visit this page : MySQL Tutorial
Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database
selected your query to select or update a table will not work.
Sometimes a web host will require you to specify the MySQL server name and port number. For example if
the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number
for MySQL) then you you can modify the above code to :
<?php
$dbhost = 'db.php-mysql-tutorial.com:3306';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'petstore';
mysql_select_db($dbname);
?>
It's a common practice to place the routine of opening a database connection in a separate file. Then
everytime you want to open a connection just include the file. Usually the host, user, password and database
name are also separated in a configuration file.
An example of config.php that stores the connection configuration and opendb.php that opens the connection
are :
<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>
<?php
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
<?php
include 'config.php';
include 'opendb.php';
?>
<?php
// an example of closedb.php
// it does nothing but closing
// a mysql database connection
mysql_close($conn);
?>
Now that you have put the database configuration, opening and closing routines in separate files your
PHP script that uses mysql would look something like this :
<?php
include 'config.php';
include 'opendb.php';
include 'closedb.php';
?>
Recent Comments
Real awesome tutorial, between this one and the one at http://bright-tutorials.com I was able to understand some
basic php code!
Great tutorial. I really appreciate it. This tutorial and the ones over at http://bright-tutorials.com really helped me with
php and mysql. Thanks!
By: zeroge Posted on 01-01-2010 4:45 AM
Great tutorial and great idea ... just in my way I get an error
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'me'@'here' (using password: NO) in
/home/bla/bal/balla/opendb.php on line 3
Unable to select database
Where "balla" is the password protected directory. Trying to open the directory itself by typing simply the URL like
htt://mydomain.com/balla/ I will be prompted for providing a username and password, though the directory seems to
work.
Why is it I get then this error when trying to call the page that hosts the "includes" ?
Thanks a lot
View All
Alone