VisageHosting.com
 
Home
 
Resources
- PHP and MySQL (2)
- HTML and CSS (0)
- CGI and Perl (0)
- Java (0)
- JavaScript (1)
- Python (0)
- ASP (1)
- Web Hosting/Servers (14)
 
Articles
- PHP/MySQL (11)
- HTML/CSS (5)
- Other Languages (4)
- Web Hosting (2)
- Computer Tech (7)
 
Forum
 
 
Register
Login

Username:

Password:

 
 
Currently 0 registered users and 5 guests browsing.

29 articles available for viewing.

1313400 total page hits.

Welcome to our newest registered user, choonchan!


PHP/MySQL -> Tutorials



Connecting to a MySQL Database via PHP

- by surreal on 05/20/06, 3:12AM CEST (4136 Views)



The most important part of linking your database to your website output is connecting to your MySQL database. In this article I will explain how this is achieved using PHP.

First you need to make sure you have a working MySQL database, and have knowledge of your database username, database name, and database password. Without these 3 essential items, it will not be possible to connect to your database at all!

The first thing we need to do is assign some variables to be used in our connection. Below is an example of what you can use.


Code:
// Database connection parameters

// MySQL database host (should almost always be localhost)
$dbhost       = 'localhost';

// MySQL database username
$dbuser = 'surreal';

// MySQL password (be sure to change this part :P)
$dbpass = 'good-secure-password-here';

// MySQL database name
$dbselect = 'juxtaposing';


Most of this is entirely self explanatory. It's essential you have a good, secure password, using a mix of letters, numbers and symbols.

The only part that needs a bit of explanation is the "$dbhost = 'localhost';". Basically this part tells the MySQL database where the connection should be coming from. "localhost" basically means the connection should be coming from the same machine that the MySQL database resides. If the connection comes from anywhere else, it will be rejected. The default will almost always be localhost, so you have nothing to worry about here.

Now we can move on to actually connecting to the database. This is done using the PHP function mysql_connect(); including the parameters we outlined previously, as shown below:


Code:
// Connect to database
if (!mysql_connect($dbhost,$dbuser,$dbpass))
       die('ERROR: Could not connect to MySQL database!');


You will notice that it has been included in an if statement. This is so we can check if the connection failed at the same time as executing it. If the connection fails, the script will terminate with the message 'ERROR: Could not connect to MySQL database!'.

On a successful connection, we now need to select which database we're connecting to. This is achieved using the nifty mysql_select_db(); function, using the paramets defined previously once again:


Code:
// Select database
if (!mysql_select_db($dbselect))
       die('ERROR: Could not select MySQL database!');


Just as before, we're checking to see if the database selection fails.

If you get no errors, you've successfully connected to your MySQL database :)

You will need to include all of the code outlined here on every script that you plan on using your database. Because of this, it may be useful to save your database connection in a separate PHP file (such as db.php) and use the require() function at the top of each page to include the database connection. This way you can manage your database connection without having to edit it on each page. Below is an example of how this can be done:


Code:
<?php

require('db.php');

// Below this goes the rest of your script


There you have it, a successful database connection!


Seen this article somewhere else? Report it for plagiarism!

Noticed an error in this article? Tell us what's wrong!

View Comments for this Article

Related Articles

- How to Create a Site Search Using MySQL and PHP

- How to Use Requires in PHP

- How to Grab a Single Field From a MySQL Table using PHP

- (Uniball/BRChat) Uniball Common Problems and Solutions

- PHP Syntax (Control Structures): while, do-while and for Loops
Rating

5.00/5

2 Votes

Want to rate this article? Register a free account or Login if you already have one.


Comments

Comment by choonchan on 08/09/10, 9:10PM CEST

LtvdvB [a href="http://bhcpmalsgwyq.com/"]bhcpmalsgwyq[/a], [url=http://vnqcfdfyyobb.com/]vnqcfdfyyobb[/url], [link=http://eimftcwpwkua.com/]eimftcwpwkua[/link], http://znwfhfvwomww.com/

Comment by lymanknap on 08/05/10, 2:02PM CEST

KgTbtp [a href="http://tbvapffobuaz.com/"]tbvapffobuaz[/a], [url=http://ibrktwgxzlsh.com/]ibrktwgxzlsh[/url], [link=http://tuajbvkfbhmd.com/]tuajbvkfbhmd[/link], http://fykxwkncglcx.com/

Comment by Li0rE on 05/20/06, 4:30AM CEST

Nice tutorial. How can I submit one?



Want to add a comment? Register a free account or Login if you already have one.



All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.