PHP Programming with MySQL Beginner's Tutorial
Register

We are the best invite forum on the internet! Here you will find free invites, free seedboxes, free bonuses, and much more. Our members know the true meaning of sharing and have created a truly global bittorent community! Our site has the most up to date information on all private trackers and our members will guide you and introduce you to this truly secretive and enlightened club. Ready to get started? Register now!


Results 1 to 1 of 1
  1. #1

    Join Date
    Jan 2011
    Posts
    364

    Default PHP Programming with MySQL Beginner's Tutorial



    In this tutorial I will teach you the basics of PHP and it's involvement with MySQL databases to which you can incorporate to many types of web development projects. This provides an absolute beginner-entry instruction to begin programming in this manner.

    It is assumed that you either have access to a server with php installed and access to MySQL databases (almost every host provides this) or have installed both on your computer. Most hosts also provide phpmysqladmin so I'm not going to teach you the manual way of creating databases and adding tables. There are plenty of tutorials if you do not already know how to do this and don't have phpmysqladmin.

    1) Basic PHP and connecting to your database

    Note: code lines starting with // are called comments and are skipped over when the file is executed. These allow me to make notes about what each line is doing.

    Making a PHP file

    All php files must end in .php. Create a file with this extension (I'm going to use test.php). In every php file you must indicate that php code is being used by the tags <?php and ?>

    PHP Code:
    <?php // indicates the start of the php code

    // everything you want executed goes in here.

    //indicates the end of the php code
    ?>
    Basic PHP Commands



    The semicolon (;) indicates the end of a line. Omitting them is a common error.

    Echo command - outputs a string of text; syntax: echo "TEXT HERE";

    Variables - a variable holds a string of information for use later. It is very important in almost every usage of php. The $ is used to indicate a variable. The syntax to create a variable is: $NameOfVariable = "Text string to be saved";

    Once information is inside a variable you can do various things with it such as math operations.

    PHP Code:
    <?php

    echo "Hello, I am about to create a variable";

    $MyVariable "My favorite number is:"//Creates a variable with a string of text in it
    $MyOtherVar 5//Creates a variable with the number 5 in it.
    $MyFinalVar $MyOtherVar 8//Makes a third variable that multiplies 8 times 5

    echo $MyVariable;
    echo 
    $MyFinalVar
    // This output "My favorite number is: 40"

    ?>


    Connecting to the Database

    Once you have set up your database, please note the name of your database and the username and password of the user attached to it. Server is localhost unless your MySQL is at a separate location in which case you will need to put that IP address.

    To connect to the database you will use the following commands:
    mysql_connect(SERVER LOCATION, USERNAME, PASSWORD);
    mysql_select_db("DB NAME");


    After these mysql commands we can add: or die() in order to get an error notifaction if something is wrong. This command can be used with a custom error message or with an automated message: die(mysql_error());

    PHP Code:
    <?php

    mysql_connect
    (localhostuser1pass1) or die("Connection failed");
    mysql_select_db("test") or die ("Database selection failed");

    ?>

    END OF PART 1

    2) Communication with Database

    Now that you know how to connect to your database, let's use it!

    Insert data into your database

    This inserts new data into a database table. You must make sure to check the name of your table and the names of the individual fields within your table. If you are unsure how to properly set up your table, please look for information about this.

    We will use this command:

    mysql_query("INSERT INTO table_name (field1, field2) VALUES ('value1', 'value2') ");

    Note: In the value spots you do not need quotes if you are using a variable.

    PHP Code:
    <?php

    mysql_connect
    (localhostuser1pass1) or die("Connection failed"); //connect
    mysql_select_db("test") or die ("Database selection failed"); //select db

    $value1 "Hello";
    $value2 "See ya";

    mysql_query("INSERT INTO phrases (greetings, goodbyes) VALUES ($value1, $value2) ") or die(mysql_error());
    // This inserts the data "Hello" into the greetings field of the phrases table
    // and "See ya" into the goodbyes field. The values are put into variables
    // and therefore quotes are not needed in the query.

    ?>


    Update and delete date already in database

    This is the syntax of our queries:

    mysql_query("UPDATE table_name SET field='value' WHERE field='previous_value'");
    mysql_query("DELETE FROM table_name WHERE field='value'");


    PHP Code:
    <?php

    mysql_connect
    (localhostuser1pass1) or die("Connection failed"); //connect
    mysql_select_db("test") or die ("Database selection failed"); //select db

    mysql_query("UPDATE phrases SET greetings='Hi' WHERE greetings='Hello'") or die(mysql_error());
    // This updates the greetings entry for "Hello" and replaces it with "Hi"

    mysql_query("DELETE FROM phrases WHERE goodbyes='See ya'") or die(mysql_error());
    // This deletes the goodbyes entry of "See ya" from the phrases table

    ?>


    This is the end of the tutorial for now but it will be expanded if it is well received. It was written by me not copy and pasted from somebody else. Please press the THANKS button if you found this helpful.
    Last edited by Brenden; January 18th, 2011 at 02:09 PM.


  2. To remove ads become VIP. Inquire about advertising here.

Similar Threads

  1. Face Recognition Using C# with MySQL
    By Black in forum Coders - Hello World!
    Replies: 0
    Last Post: July 14th, 2011, 12:50 PM
  2. Replies: 0
    Last Post: March 26th, 2009, 10:35 AM
  3. Websites with Sony Vegas Tutorials
    By learntherules in forum General Discussion
    Replies: 1
    Last Post: February 18th, 2009, 12:56 AM
  4. Which programs to integrate with Core XP v3?
    By SunSpyda in forum Computers
    Replies: 12
    Last Post: January 12th, 2009, 01:16 AM
  5. Beginner Sig [Tag] Tutorial..FOR BEGINNERS!
    By KlassicKool in forum Miscellaneous
    Replies: 3
    Last Post: September 11th, 2008, 04:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •