LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > Beginner PHP-MySql tutorial
Added on   14.11.09
Clicks     1523
Rating:   4.00   out of 5 from   2   raters      
         rate this page   
 Rate this page,Please

iBuyOfficesupply.com Inc. There is not Comments for this Article,Be the first one
Ask any relevant question in the Comments or leave your valuable feed-back

printer  Print version of this article

star  Add it to your favourite!

mail email this page to a frend
LinkShare  Referral  Program

Beginners PHP-MySql Tutorial



 Introduction


 Before you actually start building your  scripts, you must have MySql running and a database to place information into and read it from. In this Tutorials you will learn how to create a database in MySQL and prepare it for the data and also how to create a Table. The you'll learn all the basic aspects of database management via PHP including the MySql statements SELECT - INSERT - UPDATE - DELETE and most of the built in PHP function to dial with MySql.
The code below is for creating a Database and a Table with PHP,but it's much better for you to use PHPmyAdmin to do these basic tasks,if you hosting your website at home, you can download it from this page www.phpmyadmin.net/home_page/downloads.php or you can also use MySql Administrator that you can download for free when you install MySql ,if you are using hosting service they usually provide PHPmyAdmin.
    <?php
    $host = "localhost"; // your host name
    $username = "yourusername"; // your user name to access MySql
    $password = "yourpassword"; // your password to access MySql
    $database = "yourdatabase"; // The name of the database

     if (!$link  = @mysql_connect($host,$username,$password,true))
    {die('Could not connect:'. mysql_error()); }




    $sql = 'CREATE DATABASE '.$database.'';
    if (mysql_query($sql, $link)) {
        echo "Database".$database."created successfully\n";
    } else {
        echo 'Error creating database: ' . mysql_error() . "\n";
    }
    mysql_close($link);
    ?>

    Explaining the above code is quite simple, as you can see ,it starts with for variables where we store the host,the username,the password and the database name.Then we connect to MySql using a predefined PHP function mysql_connect passing as parameters the variables we created,once the connection succeeds,we can create the database assigning the SQL statement  'CREATE DATABASE $database'to the variable $sql and then using another PHP function mysql_query to execute the query and ,at the end,it's a good habit to close the connection with the PHP function mysql_close.That's it the database is ready to store all your data

    Create a Table

    Now that we've got a database we can create a table ,but before actually do it we better have a look what data the table will be storing,for our example we are going to store books for a library, Therefore we'll need an identifier , a title, an author and a category for each book,let's see how we can sort it out:

    Field Type Length Description
    ID int 6 Unique identifier
    title Varchar 250 Book's title
    Author Varchar 150 The Author's name
    Category Varchar 50 The Category for the book
    Price Varchar 20 The book price

    We can see the code below

    <?php
    $host = "localhost"; // your host name
    $username = "yourusername"; // your user name to access MySql
    $password = "yourpassword"; // your password to access MySql
    $database = "yourdatabase"; // The name of the database

     if (!$link  = @mysql_connect($host,$username,$password,true))
    {die('Could not connect:'. mysql_error()); }

    @mysql_select_db($database) or die( "Unable to select database");


    $sql='CREATE TABLE `books` (
    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `author` VARCHAR(150) NOT NULL,
    `category` VARCHAR(45) NOT NULL,
    `price` VARCHAR(20) NOT NULL,
    PRIMARY KEY (`id`)
    )
    ENGINE = InnoDB;


    mysql_query($query);
    mysql_close();

    )';

    The code creates a table in a existing database,it starts like before ,declaring all the variables ,the i connects to the database link mysql_connect('$host', '$username', '$password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    And this time we select the Database to store the table using the PHP function @mysql_select_db , we assign the sql statement to create the book table and then we execute the query with mysql_query,and finally,we close the connection.

    Try the example below