Mysql Starter Tut - Part 2

Connecting Your First SQL Database

In this tutorial you will learn how to connect to your first Mysql Database using PHP and its PHP functions. You will need to know the basic knowledge of how to use PHP.

Step 1 - Understanding the “mysql_connect” PHP Function

PHP makes it easy to use mysql in its applications because it already has built in mysql functions allowing you to connect to your database with ease.

Connecting to a database is really simple. It contains 3 parameters that you will need to connect to any database.

$link = mysql_connect(host,user,password);

Above is some pseudo code (example code, that might not run correctly if executed) that contains our basic usage of connecting to a database. “host”,”user”,”password” are parameters. What is a parameter? A parameter is something that goes between the parenthesis of a PHP function. It is used to predefine function settings, that the user creates or already made functions use, to make a function work in a different way.

1. First we define a PHP variable and we use an equals sign to assign out mysql connect function to it for latest use to check if we connected okay.

2. Our mysql_connect function contains three parameters. The first one will be “localhost”. We are assuming that your sql server is on your current hosting account and localhost means on your local hosting machine. Not a mysql server on another server machine. Second parameter is the database user name, remember in Mysql Starter Tut - part 1 when we created our first database user and database? Usually your mysql username that you created will be formated like: “cpanelname_usernameyoucreatedincpanel”. The last parameter is the mysql username’s password you created in part 1. I should look something like below:

$link = mysql_connect(”localhost”,”cpanelname_username”,”userpassword”);

Step 2 - What if we didn’t connect to mysql database?

Well thats simple we add a check to our line of php code. We use a function called “die” it’s use is to shut down the rest of the php functions after from executing and outputting an error message. There’s only one parameter we add and thats the message parameter. We apply it to our line of php code like below.

$link = mysql_connect(”localhost”,”cpanelname_username”,”userpassword”) or die(”couldn’t connect”);

Above works like this. Lets try connecting to our database, if there aren’t any errors return success else if there was an error output a message telling the visitor or programmer they couldn’t connect to the database and stop the rest of the code from executing, because it might spit out more errors then we want to see since many applications rely on mysql connections.

Step 3 - Selecting our active database

We want to select our active database because if we don’t we could possibly have code not run right, or if not selecting the right database applications might get written over when you don’t want them to be.

PHP comes with a function that does this for us called mysql_select_db. It only contains one parameter, but an optional parameter for a resource link identifier that we created in step 2.

Whats a Resource Link Identifier?

Well in step two we created a successful connection to our database. The $link variable stores a resource of the mysql connection which we use to select our active database. This is optional in php, but some might argue that this is a more accurate way of setting up sql connections.

Selecting our database

$db = mysql_select_db(”cpanelname_databasename”,$link) or die(”couldn’t select database”);

Above is how we connect to our database. Like in step 2 we added the die function to make sure we connect to the database successfully.

Outcome

<?php

$link = mysql_connect(”localhost”,”cpanelname_username”,”userpassword”) or die(”couldn’t connect”);

$db = mysql_select_db(”cpanelname_databasename”,$link) or die(”couldn’t select database”);

?>

Thanks basically how you connect to a database. You could close connection using mysql_close at the very ends of your script, but people think it’s bad programming practice.

Continue Reading

Mysql Starter Tut - Part 3 - where you learn how to create your first mysql table.

-->

4 Responses

  1. Mysql Starter Tut - Part 1 | MySQL 5 Tutorials & Articles Says:

    [...] Mysql Starter Tut - Part 2 where you learn how to connect to a database using mysql and php. [...]

  2. Mysql Starter Tut - Part 3 | MySQL 5 Tutorials & Articles Says:

    [...] PHP allows you to execute mysql queries(mysql code) with ease.  Using it’s basic function mysql_query which takes one parameter we can execute our code and our newly create mysql table will be create. Remember to always connect to your database before executing mysql queries. If you don’t know how to please read Part 2 of Mysql Starter Tut. [...]

  3. Tyler Ingram Says:

    One thing I like to do when setting up a MySQL Connection is to put the connetion arguments (host, pass, user, db) into seperate variables so that I can easily find them and only have to edit them once if I need to.

    Of course I also have a simple MySQL Class too but that’s a bit more than the above.

    Good job though. Easy to follow!

  4. admin Says:

    Thanks for your input. I find using classes easier my self. Specially on a factory class where you want to connect to different types of sql databases like sqlite etc..

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.