Dale Hay . com
Menu
Home
About
Forum
Contact
PHP Tutorials
BBCode
For Loop
Image Uploader
Message Board
Search Engine
Session Login
vidDetect
Photoshop Tutorials
Easy Clipart Style
Tech Stuff
HTML Friendly
MD5 Encrypter
SHA1 Encrypter
TGP Generator
Video Downloader
Web Affiliates
Web Linkbacks
Web Spam Emails
Random
Utilities
Learn Piano
Twinkle, Twinkle

PHP Tutorial - MySQL Connect

Digg! This Page

Introduction

This tutorial will teach you, very simply how to connect to your database and show an error if it doesn't connect correctly.

Step 1

First of all we need to open our PHP tags.
Code:
<?php

?>

Step 2

Now we'll need to start by using a function called mysql_connect(). It hold 3 properties (1st - Database Hostname (eg; localhost in this tutorial), 2nd - Database Username (eg; foo in this tutorial), 3rd - Database Password (eg; bar in this tutorials)).
Code:
<?php
mysql_connect("localhost","foo","bar");
?>

Now just incase we have an error with those details we entered you can add a "or die" option at the end of it.
Code:
<?php
mysql_connect("localhost","foo","bar") or die("There was an error trying to connect to the database");
?>

This will then show an error message and stop the rest of the mysql stuff from loading if it cannot connect.

Step 3

Ok, so now we have connected to the database, we need to connect to the table in the database using the function mysql_select_db, which has one property (1st - table name (eg; dalehay in this tutorial)) - To find out what the table name is, just log into your phpMyAdmin and check the name at the top of the fields in the left menu (it usually has a number in brackets next to it).
Code:
<?php
mysql_connect("localhost","foo","bar") or die("There was an error trying to connect to the database");
mysql_select_db("dalehay");
?>

Now obviously if there IS a problem with that line, we also want it to stop and show an error message, so just like before add a "or die" tag at the end like so:
Code:
<?php
mysql_connect("localhost","foo","bar") or die("There was an error trying to connect to the database");
mysql_select_db("dalehay") or die("Hmmm the hamsters couldn't seem to connect to the database table");
?>

Nice and simple. :-)

Comments

Comments:

Add your comment...

Your Name:

Your Email:

Your Comment:

Enter This Number:

(29389)


Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!


Links
Audio Video Cables
My Other Sites
Convo World
Sound Upload
UKtInfo
Budz
Feminine Hijinx
Martyn Coleman . com
SurfiOnline
Total Talent
Linux HOWTOs, Tutorials & Projects with Adam Palmer
Programs
dhSpy
Dale's Converter 1.1.1
DuMP
Other Links
Old Host: Servage
Latest News Stories
» Snow!
» Merry Christmas
» Sound Upload Progress
» R.I.P Sound Upload
» Basshunter - I Miss You (By Lynn)
» No on 8!
Top Viewed Stories
» Bypass Photobucket (28,194)
» Rare McDonalds Monopoly 2008 Tickets (8,331)
» PHP Mini Message Board Tutorial (7,630)
» PHP Search Engine Tutorial (4,238)
» Helen Willetts Pregnant? (2,958)
» Create your own Image Uploader (1,926)
Most Commented Stories
» PHP Mini Message Board Tutorial (46)
» Create your own Image Uploader (19)
» I like my DuMP (15)
» PHP Search Engine Tutorial (10)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)



Copyright Dale Hay, © 2005 - 2008