PHP AND MYSQL
Index
1. What is php?
2. Uses of php
3. Basic syntax of php
4. Variables
5. Echo/print statement
6. Array
7. Decision making
8. Loops
9. Functions
10. Mysql connections to php
11. Framework
What is php ?
 Php is scripting language commonly used on web servers.
 stands for Php: Hypertext preprocessor.
 open source.
 Embedded code.
 Multiple operating systems/web servers.
Uses of php
 Programming functions.
 You can add, delete, modify, etc within database through php.
 Encrypt data.
 Using PHP, you can restrict users to access some pages of your
website.
Basic php syntax
<html>
<head>
<title></title>
</head>
<body>
<?php
echo “Hello!PHP”; //prints Hello!PHP
?>
</body>
</html>
 Comment: To make the single line comment use //.To make large
comment block use /* and ends with */.
Example:
<?php
//this is comment
/*this is comment
Block*/
?>
Variables
 Php variables start with $ sign and followed by name of variable.
 Case sensitive($FOO!=$foo!=fOo)
 Globally and locally scoped variable
 Global variables can be placed anywhere.
 Local variables restricted to a function or class.
 Certain variables are reserved by php
 Form variables($_post,$_get)
 Server variables($_server),etc.
Variables usage
Example:
<?php
$txt = "Hello world!";
$x = 5;
$y = 5.44;
echo $txt; // prints Hello world!
echo ”br”;
echo $x; // prints 5
echo “br”;
echo $y; // prints 5.44
?>
Echo/Print
 There are two ways to print the output: echo and print
Example:
<?php
$foo = 25;
$bar = “Well come to php”;
echo $bar; //prints Well come to php
echo $foo; //prints 25
echo $foo,$bar; //prints 25hello
?>
Arrays
 An array is a special variable that stores more than one element at
a time.
 In php, there are three types of array:
. Indexed Array
. Associative Array
. Multidimensional Array
Arrays(contd..)
1.Indexed array: An array with numeric index.
Example:
$cars = array("Volvo", "BMW", "Toyota");
or
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Arrays(contd..)
2.Associative array: An array where each ID key is associated with a
value.
Example:
$salary = array(
"Peter"=>“2000",
"Ben"=>“3000",
"Joe"=>“4000“ );
Arrays(contd..)
3.Multidimensional array: An array containing one or more arrays
and values are accessed using multiple indices.
Example:
<?php
$letters = array('A','B','C'); $numbers =
array(1,2,3);
$matrix = array('Letter' =>$letters,
'Number' => $numbers);
?>
Operators
 Arithmetic operator
 Logical operator
 Assignment operator
 Comparison operator
 String operator
$total=$x+$y
$x and $Y
$z=$x+$yor(=+,=-)
$x==$y
$txt1.$txt2
Php- if..elseif..else
if (condition1) {
//code to be executed;
} elseif (condition2) {
//Code to be executed;
} else {
//code to be executed if neither;
// condition1 nor condition2 is true;
}
Php-Switch statement
$favcolor=“red”;
switch($favcolor){
case “red”:
echo ” your favorite color is red”;
break;
case “blue”:
echo ” your favorite color is blue”;
break;
default:
echo “ ..neither red or blue ”;
Loops
 while:
$x=1;
while ($x<=5){
echo “Number: $x</br>”; $x++;
}
dowhile: $x=1;
do
{
echo ” Number: $x</br>”; $x++;
}while ($x<=5);
Loops(contd)
 For Loop:
<?php
for ($x=0; %x<=10; $x++)
{
echo “ The number is: $x </br> ”;
}
?>
Loops(contd)
 foreach Loop: The foreach loop works only on arrays, and used to
loop through keys/value pair in an array.
<?php
$color=array(“red”,”green”,”blue”;
foreach ($color as $ value)
echo “$value</br>”;
?>
Functions
 A Function is a self-contained block of code that performs to
perform specific task.
 Php Built-in functions.
 Php User-defined functions.
function functionName(){
// code to executed;}
 Php functions with Parameters.
function functionName($parameter1,$parameter2){
//Code to executed; }
Mysql connection
 Create connection: Before we can access data in the MySQL database, we
need to be able to connect to the server:
$conn = new mysqli($servername, $username, $password);
 Close connection: The connection will be closed automatically when the
script ends.
$conn->close();
Mysql database
 Create database: The CREATE DATABASE statement is used to
create a database in MySQL.
$sql = “create database myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
Mysql connection
 Create table: The create table statement is used to create a table
in MySQL.
create table MyGuests (
id int(6) unsined
auto_increment primary key,
firstname varchar(30) not null,
lastname varchar(30) not null,
email varchar(50),
reg_date timestamp
);
Mysql database
 Insert values: After creating the database and table start inserting
the values to the database.
INSERT INTO table name (column1, column2,
column3,...)VALUES (value1, value2, value3,...);
 Update data: The UPDATE statement is used to update existing
records in a table.
Update table_name
SET column1=value, column2=value2,...
WHERE some column=some_value
Frameworks
A Frame is a set of common and prefabricated software building
blocks that programmers can use, extend customize for specific
computing solutions.
1. Code Igniter.
2. CakePHP.
3. PHPDevShell.
4. Akelos.

Php

  • 1.
  • 2.
    Index 1. What isphp? 2. Uses of php 3. Basic syntax of php 4. Variables 5. Echo/print statement 6. Array 7. Decision making 8. Loops 9. Functions 10. Mysql connections to php 11. Framework
  • 3.
    What is php?  Php is scripting language commonly used on web servers.  stands for Php: Hypertext preprocessor.  open source.  Embedded code.  Multiple operating systems/web servers.
  • 4.
    Uses of php Programming functions.  You can add, delete, modify, etc within database through php.  Encrypt data.  Using PHP, you can restrict users to access some pages of your website.
  • 5.
    Basic php syntax <html> <head> <title></title> </head> <body> <?php echo“Hello!PHP”; //prints Hello!PHP ?> </body> </html>
  • 6.
     Comment: Tomake the single line comment use //.To make large comment block use /* and ends with */. Example: <?php //this is comment /*this is comment Block*/ ?>
  • 7.
    Variables  Php variablesstart with $ sign and followed by name of variable.  Case sensitive($FOO!=$foo!=fOo)  Globally and locally scoped variable  Global variables can be placed anywhere.  Local variables restricted to a function or class.  Certain variables are reserved by php  Form variables($_post,$_get)  Server variables($_server),etc.
  • 8.
    Variables usage Example: <?php $txt ="Hello world!"; $x = 5; $y = 5.44; echo $txt; // prints Hello world! echo ”br”; echo $x; // prints 5 echo “br”; echo $y; // prints 5.44 ?>
  • 9.
    Echo/Print  There aretwo ways to print the output: echo and print Example: <?php $foo = 25; $bar = “Well come to php”; echo $bar; //prints Well come to php echo $foo; //prints 25 echo $foo,$bar; //prints 25hello ?>
  • 10.
    Arrays  An arrayis a special variable that stores more than one element at a time.  In php, there are three types of array: . Indexed Array . Associative Array . Multidimensional Array
  • 11.
    Arrays(contd..) 1.Indexed array: Anarray with numeric index. Example: $cars = array("Volvo", "BMW", "Toyota"); or $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";
  • 12.
    Arrays(contd..) 2.Associative array: Anarray where each ID key is associated with a value. Example: $salary = array( "Peter"=>“2000", "Ben"=>“3000", "Joe"=>“4000“ );
  • 13.
    Arrays(contd..) 3.Multidimensional array: Anarray containing one or more arrays and values are accessed using multiple indices. Example: <?php $letters = array('A','B','C'); $numbers = array(1,2,3); $matrix = array('Letter' =>$letters, 'Number' => $numbers); ?>
  • 14.
    Operators  Arithmetic operator Logical operator  Assignment operator  Comparison operator  String operator $total=$x+$y $x and $Y $z=$x+$yor(=+,=-) $x==$y $txt1.$txt2
  • 15.
    Php- if..elseif..else if (condition1){ //code to be executed; } elseif (condition2) { //Code to be executed; } else { //code to be executed if neither; // condition1 nor condition2 is true; }
  • 16.
    Php-Switch statement $favcolor=“red”; switch($favcolor){ case “red”: echo” your favorite color is red”; break; case “blue”: echo ” your favorite color is blue”; break; default: echo “ ..neither red or blue ”;
  • 17.
    Loops  while: $x=1; while ($x<=5){ echo“Number: $x</br>”; $x++; } dowhile: $x=1; do { echo ” Number: $x</br>”; $x++; }while ($x<=5);
  • 18.
    Loops(contd)  For Loop: <?php for($x=0; %x<=10; $x++) { echo “ The number is: $x </br> ”; } ?>
  • 19.
    Loops(contd)  foreach Loop:The foreach loop works only on arrays, and used to loop through keys/value pair in an array. <?php $color=array(“red”,”green”,”blue”; foreach ($color as $ value) echo “$value</br>”; ?>
  • 20.
    Functions  A Functionis a self-contained block of code that performs to perform specific task.  Php Built-in functions.  Php User-defined functions. function functionName(){ // code to executed;}  Php functions with Parameters. function functionName($parameter1,$parameter2){ //Code to executed; }
  • 21.
    Mysql connection  Createconnection: Before we can access data in the MySQL database, we need to be able to connect to the server: $conn = new mysqli($servername, $username, $password);  Close connection: The connection will be closed automatically when the script ends. $conn->close();
  • 22.
    Mysql database  Createdatabase: The CREATE DATABASE statement is used to create a database in MySQL. $sql = “create database myDB"; if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; }
  • 23.
    Mysql connection  Createtable: The create table statement is used to create a table in MySQL. create table MyGuests ( id int(6) unsined auto_increment primary key, firstname varchar(30) not null, lastname varchar(30) not null, email varchar(50), reg_date timestamp );
  • 24.
    Mysql database  Insertvalues: After creating the database and table start inserting the values to the database. INSERT INTO table name (column1, column2, column3,...)VALUES (value1, value2, value3,...);  Update data: The UPDATE statement is used to update existing records in a table. Update table_name SET column1=value, column2=value2,... WHERE some column=some_value
  • 25.
    Frameworks A Frame isa set of common and prefabricated software building blocks that programmers can use, extend customize for specific computing solutions. 1. Code Igniter. 2. CakePHP. 3. PHPDevShell. 4. Akelos.