What Is PHP

What is PHP?

  • PHP (recursive acronym for PHP: Hypertext Preprocessor)
  • PHP is a server-side scripting language, like ASP
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
  • PHP is an open source software
  • PHP is free to download and use

What is a PHP File?

  • PHP files can contain text, HTML tags and scripts
  • PHP files are returned to the browser as plain HTML 
  • PHP files have a file extension of ".php", ".php3", or ".phtml"

What is MySQL?

  • MySQL is a database server
  • MySQL is ideal for both small and large applications
  • MySQL supports standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use

PHP + MySQL

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

1. Installing a Webserver with PHP and MySQL (XAMPP in Windows)

Installing a Webserver with PHP and MySQL (XAMPP in Windows) :
  • Installing XAMPP in Windows
  • XAMPP is a cumulative package consisting of Apache, PHP and MySQL Packages is available both for windows and linux
  • In this tutorial the XAMPP will be installed and the default webserver directry will be "htdocs".
Xampp Download :
http://www.apachefriends.org/en/xampp.html

2. Echo PHP Function, PHP Variables, If and Switch Statements

Echo Function :
  • The echo() function outputs one or more strings.
  • Syntax: echo(strings);
  • Ex. echo "Hello World!";
http://pastebin.com/b9xc54uc
Variables in PHP :
  • Variables are used for storing values, like text strings, numbers or arrays.
  • When a variable is declared, it can be used over and over again in your script.
  • All variables in PHP start with a $ sign symbol.
  • The correct way of declaring a variable in PHP: $var_name = value;
http://pastebin.com/aBkwyB4K
 If Statement :
  • if statement - use this statement to execute some code only if a specified condition is true.
  • if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false.
  • if...elseif....else statement - use this statement to select one of several blocks of code to be executed.
http://pastebin.com/WQMGVtjz
Switch Statement :
  • switch statement - use this statement to select one of many blocks of code to be executed
http://pastebin.com/mGngHSsq

3. PHP Operators

Arithmatic Operators :
  • Ex. +,-,*,/,%,++,--
http://pastebin.com/VBXuG5xT
Comparison Operators :
  • Ex. ==,!=,<>,>,<,>=,<=
http://pastebin.com/eBgcTdVv
Logical Operators :
  • Ex. && (AND),|| (OR),! (NOT)
http://pastebin.com/h1ESjRqX

4. Arrays in PHP

Arrays :
  • An array stores multiple values in one single variable.
  • Numeric array - An array with a numeric index.
  • Associative array - An array where each ID key is associated with a value.
  • Ex. Numeric Array: $fruits=array("Apple","Mango","Banana","Grapes");
http://pastebin.com/SwJH96b1
Multi-Dimensional Arrays :
  • In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
http://pastebin.com/eVEF0WZM

5. Loops in PHP

Loops execute a block of code a specified number of times, or while a specified condition is true.

Loops - While Statement :   
  • The while loop executes a block of code while a condition is true.
  • while (condition)
    {
    code to be executed;
    }
[ Code ] : 
http://pastebin.com/EX7L8f4J

For More loops Click On READ MORE

6. Functions in PHP

Functions (Basic) :
  • To keep the script from being executed when the page loads, you can put it into a function.
  • A function will be executed by a call to the function. 
  • You may call a function from anywhere within a page. 
  • Syntax:
    function functionName()
    {
    code to be executed;
    }

[ Code ] :
http://pastebin.com/yNii9Gya

Functions (Advanced) :
  • We can also pass parameters to functions during both the declaration and calling time.
  • function functionName($param1,$param2); //during function call.
  • function functionName($param1,$param2) 
  • {
    code to be executed
    }     
[ Code ] : 
http://pastebin.com/hCeJ4Kmk

7. PHP Special Variables

GET Variable :
  • The built-in $_GET function is used to collect values from a form sent with method="get".
  • Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar)  
  • It has limits on the amount of information to send.
[ Code ] :
http://pastebin.com/6kfYqfvq

POST Variable :
  • The built-in $_POST function is used to collect values from a form sent with method="post".
  • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.  
[ Code ] : 
http://pastebin.com/FGazFkEY

8. PHP and HTML

 Embedding PHP
  • We can embed our PHP code anywhere in the webpage, by enclosing our script within the <?php...... //SCRIPT.......?>  
[ Code ] :
http://pastebin.com/m2crcKG3

Common Way to Display HTML
  • We can also use the HTML Code within the PHP Script. Almost each of the HTML Tags can be used within a PHP Script.

[ Code ] :
http://pastebin.com/JSXznzQ7

MYSQL Tutorials: Level 2

MySQL is a Relational Database Management System (RDBMS) that runs as a server providing multiuser access to a number of databases. A third party open source software "phpMyAdmin" will be used as a web-based front end for managing MySQL databases easily and efficiently. It is widely installed by Web hosts worldwide, since it is developed in PHP and is included in the convenient LAMP stack, MAMP, and WAMP software bundle installers.

MySQL (Part 1)

  • An Introduction to the PHPMyAdmin Interface. 
  • Creating a New Database  
  • Creating a new Table and entering the value of the field with the requisite datatype.
  • SQL Query displayed in the PHPMyAdmin window.

MySQL (Part 2)

  • Connecting to the database and inserting dummy data into the database. 
  • mysql_connect("server_addr", "username", "password") - Connect to the Database Server with the authorised user and password.  
  • mysql_select_db("database_name") - Selecting a database within a connected database server. 

MySQL (Part 3)

  • Writing some data into the database (INSERT and UPDATE Queries). 
  • mysql_query('TYPE_HERE_YOUR_MYSQL_QUERY') - This function is used to run specific queries on our database.  
  • INSERT QUERY - INSERT into table values ('att1', 'att2' , 'att3', 'att4' ,'att5') //Inserts Data into the table 
  • UPDATE QUERY - UPDATE table_name SET att1='xyz' //Updates the Existing values stored in the table of the database.

MySQL (Part 4)

  • Getting data from the database table and displaying it. 
  • SELECT QUERY - SELECT * FROM table_name WHERE att1='abc' // Query returns the value from the database where att1 = abc  
  • mysql_num_rows() - Gives us the number of rows there are in the query we have just given out. 
  • ORDER BY - Helps to order the output result as when selecting the values form the database.
  • {Use of DESC for Descending ordering / ASCfor Ascending ordering}  

MySQL (Part 5)

  • mysql_fetch_assoc — Fetch a result row as an associative array. 
  • array mysql_fetch_assoc ( resource $result ) //Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. 

MySQL (Part 6)

  • Getting data from the database with the help of an HTML form.
  • Creating a FORM where a user can specify a name and selecting the appropriate value from the database.  

MySQL (Part 7)

  • Changing the existing values of the databse table using HTML Forms.
  • Update unique records using the id than individual values.  

MySQL (Part 8)

  • DELETE QUERY - To Delete the specific or all the entries of the Database.
  • DELETE FROM table_name WHERE field='xyz' // Deletes the entry from the database where the field = xyz.