Connecting to Database – Part 1 of 3 PHP Database

Learn how to connect your database through a simple PHP code and see what you can do a lot of and with this code. Try it out and let me know how everything goes, good luck young one.
Many people may already know what a database is (i.e. MySQL, MsSQL, Oracle, etc.). But how exactly do you get connected to a database through PHP script, that is what this tutorial will show you, here is a list of requirements to achieve this task:

— Common PHP Knowledge
— Text Editor (Notedpad Suggested)
— MySQL Database (phpMyAdmin Script Recommended for FTPs)
— FTP (SmartFTP Would Be GREAT)
— Learning Mode (Uh….. Worry About it Later)

If you got all them, then you are ready to start up on the project/tutorial we have planned for you, now be ready because this may be a little long, so please bare with us as we jump into another world, the PHP world.

Now first off, the explanation of a database is simple and short, it is a PHP follow up that stores all information for PHP in it, and can be edited through it. Now most of you are probably asking yourselves, what is that gibberish supposed to mean. Last time, it is a PHP database that is used to store certain PHP information. Now did you get that, GREAT!

OK, first off, contact your hosting admin and ask them to created you a database, and ask them to install phpMyAdmin for you if you are not familiarized with this type of thing. Once they are done, ask for your database information (Database Name, Username, Password, and Host – generally Host would stay at localhost). Once done with that let’s fly off to our next stop, the process.

Moving on, it is that time that we begin our coding process, simply open your notepad on your computer and put in the following code:

<?php

/*
Start off easy, easy, here we go – now it is time to process this bad boy
*/

$connect_to_db = mysql_connect (“localhost” , “username” , “password” , “DB name”);

/*
Break for a second, now DB name would not be filled in with MySQL or Oracle or whatever the databases REAL NAME is, it generally is the same thing as your username, in example, if your username was exdb_example, then basically exdb_example is the DB name to, localhost is the database host, you can leave that alone cause that is generally the real host, and change the password to your correct pass, now back to the program already in session.
*/

if ($connection) {
print=(“Your are now successfully connected to the database<br>”};
} else {
print=(“Your have failed to connect to the database<br>”};
?>

Now save the file as dbconnect.php, and then place it in your MAIN INDEX.

After that, let’s take a break, 5-4-3-2-1, OK BREAK OVER. Now let’s add the code to another page we are going to call dbinfo.php, this is where all the information will go which we will have more information that you can add later in my tutorial sessions.

Now that you have an open fresh new notepad, keep in mind that this will be saved as dbinfo.php, now here we go, copy and paste this:

<table border=”1″ cellpadding=”0″ cellspacing=”0″ bordercolor=”#AAAAAA”>
<tr>
<td>
<?php

/*
On your marks, get set, PROCESS THE DATABASE(S) NOW
*/

include=(“dbconnect.php”);

?>
</td>
</tr>
</table>

Magical wizard huh, now you can save it as dbinfo.php and upload it to your main directory using your FTP, save it by the way. That is it.

====Results====

The results should of turned out amazing, the way we figure that it should is that you should see a block that comes up and says that you have been connected to the database, if so you did it right, if not, then try it again and again to see what happen, if it still persists, contact me at babyboy21@fusemail.com. Thanks, and look for “Creating DB’s and SQL Queries” soon.

Leave a Reply