Welcome to   
"The Blog"

Wednesday, May 07, 2008

Posted in PHP

Blog/News Tutorial 02

Connecting to the Database

Last tutorial I showed you how to create the database with phpAdmin.  Now I’m going to show you how to connect to the database with some PHP Code.

The first thing you need to familiarize yourself with is variable and MySQL statements.  I’ll go over it briefly but here are some great sites that can help you with everything. 

PHP Variables | MySQL Statements

Variables

A variable is really easy to understand so I’ll explain it by example.

$var = 5

Simply put, the $var is the variable and that variable is equal to 5.  Variables in PHP always start with a dollar sign ($) followed by either a letter or underscore.  Variables are also case sensitive. 

So this:

$var = 5

Is not the same as this:

$VAR = 5

And if you had some code where your variable was $varbut you called it afterwards as $Var, it would not work. Remember this because if you don’t you will have many headaches trying figure something out only to realize you had a capital letter in it.  Trust me.

Note:  To make things easy, you should always try to use lowercase variables.

MySQL Statements

I can’t get into all the SQL statements because there are too many and I don’t know them all.  I’m just going to go over the ones that we will be using.  Once again, for detailed information on this, click the links above but here’s the quick summary.

The statements we’ll be using are as follows:

SELECT– This one will be used the most.  In our app, it is used to call the database and show the information.
INSERT– If you can’t guess, this statement will be used to insert data into our database.
DELETE– Yes, you are correct.

Those are pretty much the ones we’ll be using most, though DELETE not too much but we’ll get into that.  As we get further into the tutorials I’ll explain more on these statements.  After all, I think it would be much easier to understand them once you see them in use. MOVING ON!

Connecting to the Database

Connecting to the database is really simple and only requires 2 lines of code.  Here they are:

$con = mysql_connect("your MySQL server", "MySQL User Name", "PassWord") or  die(mysql_error());

mysql_select_db("Name of the database", $con) or die(mysql_error());

It’s beautiful isn’t it?  This is like the command center of your application.  Nothing is going to work unless you use the above code to connect to the database.  Here’s the break down:

$con– This is the variable and it equals the following statements.

mysql_connect("your MySQL server", "MySQL User Name", "PassWord") – mysql connect is a PHP statement that will connect to your database.  You have to use this or else you won’t be able to connect.  In the parenthesis lies the information you need to connect to your database.  Your MySQL server is usually “localhost”.  If that doesn’t work then contact your host to find out what it is.  It could be an IP or a web address.  MySQL Username is exactly that, so is the PW.  You have to set that up with your host either through a control panel or support or something. 

or  die(mysql_error()); - or means that if the first part doesn’t work go to the second part. ‘die’ will stop your statement from continuing and within the parenthesis is what will happen when your statement ‘dies’.  ‘mysql_error()’ is more PHP syntax.  This will tell you what went wrong and where the error is.

mysql_select_db – This is another PHP Syntax that will tell the server which database to select.
Everything else is self explanatory.

Note: You have to end every PHP line with a semicolon (;) or it won’t work.  This tells the server where the statement ends.

Summary

Excellent!  So now we are somewhat familiar with variables, MySQL statements and how to connect to the database.  I’ll explain the statements and variables more and more the deeper we get into the tutorials.  I just wanted to touch on it so you have an idea.  If you want to look deeper into it, please check out those sites.  They helped me tons when I was trying to figure stuff out.

If you have any questions or want to comment on anything; the form is below!  Stay tuned for the next tutorial, we’ll move on to the dynamic news section!


 

What do you think of "Blog/News Tutorial 02"?

There are no comments posted.


Enter Your Nickname

Enter A Comment

What is two plus two?

 

Blog Categories

Misc (3)

Personal (3)

Design (2)

Internet Marketing (6)

PHP (3)


Latest Blogs

» Why do I need a website
» Site Updates and Stuff
» Google Chrome Browser
» I'm Moving!
» A Designers Nightmare

Some websites I've done...

SureFire Web Services Baby Tutors Ester-C Karma Bars
ZoAir ATS JS American Container Concepts