Welcome, Guest. Please login or register.
Did you miss your activation email?  



MySQL Tutorial

You may ask, why do I need a database? When storing information, you will want to use a database. A good example is if you were running a business website, and you wanted to store the names of all your employees so they can be printed out on a page, or accessed another way. This tutorial will be a MySQL with PHP tutorial, so, if you have not learned the basics of PHP, you may want to go take a look at that tutorial. I will list a few tools that will help you with this tutorial.

  1. phpMyAdmin
  2. MySQL Administrator
  3. MySQL Query Browser

When using PHP, if you wanted to select,insert,edit/change, or query anything from a database, you will need the MySQL connect, and select lines. The following code uses mysql_connect to connect to our database, and mysql_select_db to select our database.



Connecting and Selecting our database using MySQL
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die(mysql_error());

Before we go on to inserting info into a database, we need to look at the basic structure of a database. Below is an image of just that.



Each of the columns: id, joketext, and jokedate each hold the values that a MySQL query inserts into them. The id field should be "auto_increment", so that every time another row is submitted the number will increment once. Now that we have a basic understanding of a database, lets look at the code that inserts data into it.


<?php

$conn = mysql_connect("localhost","username","password");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $conn);

mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Bob', 'Jenning', '40')");

// Closes our connection
mysql_close($conn);

?>

In part two I will explain the above code

Continue to part two »