4. your name
your
caption
here
Setting up a Development Server
What we need.....?
1) Apache Web Server (Includes PHP)
2) MySql Database Server
3) MySql Administation Software (PhpMyAdmin)
We could install all this by hand...
All-In-One Pre-Packaged
LAMP - Linux, Apache, MySQL, and PHP
Prefer: MAMP on Macintosh XAMPP on Windows
5. your name
your
caption
here
Programmer Editor
Do not use Word, TextEdit, or NotePad
Programmer Editor
Linux: Gedit, Bluefish, Geany
Windows: NotePad++
Macintosh: TextWrangler
Feel free to use an Integrated Development
Environment such Eclipse, etc. if you have experience.
6. your name
your
caption
here
The History of PHP
PPHHPP-- PPHHPP HHyyppeerrtteexxtt PPrreepprroocceessssoorr
RRaassmmuuss LLeerrddoorrff ((11999944))
NNoott aa ttrraaiinneedd ccoommppuutteerr sscciieennttiisstt
CCoonnssuullttaanntt bbuuiillddiinngg ddyynnaammiicc wweebb ssiitteess -- ggoott ttiirreedd ooff
ddooiinngg tthhee ssaammee tthhiinngg oovveerr aanndd oovveerr iinn CC
RReeuussaabbllee bbiittss ++ HHTTMMLL TTeemmppllaatteess
8. your name
your
caption
here
Features of PHP
HHTTTTPP aauutthheennttiiccaattiioonn wwiitthh PPHHPP
CCooookkiieess
SSeessssiioonnss
DDeeaalliinngg wwiitthh XXFFoorrmmss
HHaannddlliinngg ffiillee uuppllooaaddss
UUssiinngg rreemmoottee ffiilleess
CCoonnnneeccttiioonn hhaannddlliinngg
PPeerrssiisstteenntt DDaattaabbaassee CCoonnnneeccttiioonnss
SSaaffee MMooddee
CCoommmmaanndd lliinnee uussaaggee —— UUssiinngg PPHHPP ffrroomm tthhee ccoommmmaanndd lliinnee
GGaarrbbaaggee CCoolllleeccttiioonn
DDTTrraaccee DDyynnaammiicc TTrraacciinngg
9. your name
your
caption
here
<!DOCTYPE html>
<html>
<body>
<h1> Hello from BECBGK''s home Page </h1>
<p>
<?php
echo "<h2>","My first PHP script...!","</h2>";
$sum = 30 + 40;
echo "<h2>","The sum of 30 and 40 is $sum","</h2>";
?>
</p>
</body>
</html>
My First PHP Script
10. your name
your
caption
here
<!DOCTYPE html>
<html>
<body>
<h1>Hello from BECBGK''s home Page</h1>
<p>
<?php
echo "<h2>","My first PHP script...!","</h2>";
$sum = 30 + 40;
echo "<h2>","The sum of 30 and 40 is $sum","</h2>";
?>
</p>
</body>
</html>
My First PHP Script
11. your name
your
caption
here
PHP From the Command Line
You ccaann rruunn PPHHPP ffrroomm tthhee
ccoommmmaanndd lliinnee -- tthhee oouuttppuutt
ssiimmppllyy ccoommeess oouutt oonn tthhee
tteerrmmiinnaall
IItt ddooeess nnoott hhaavvee ttoo bbee ppaarrtt
ooff aa rreeqquueesstt--rreessppoonnssee ccyyccllee
<?php
echo("Hello
World!");
echo("n");
?>
19. your name
your
caption
here
Associative Arrays
Py Like Pytthhoonn DDiiccttoonnaarriieess++LLiissttss -- bbuutt mmoorree
ppoowweerrffuull
CCaann bbee kkeeyy ==>> vvaalluuee oorr ssiimmppllyy iinnddeexxeedd bbyy
nnuummbbeerrss
IIggnnoorree ttwwoo--ddiimmeennssiioonnaall aarrrraayyss ffoorr nnooww......
20. your name
your
caption
here
Integer Indices
<?php
$stuff = array("Hi", "There");
echo $stuff[0], "n";
echo $stuff[1], "n";
?>
Hi
There
21. your name
your
caption
here
Integer Indices
<?php
$stuff = Array();
$stuff[] = "Hello";
$stuff[] = "World";
echo $stuff[1], "n";
?>
World
22. your name
your
caption
here
Key / Value
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age[''Peter''] . " years old.";
?>
23. your name
your
caption
here
Dumping an Array
The function print_r() dduummppss oouutt PPHHPP ddaattaa -- iitt iiss uusseedd mmoossttllyy ffoorr
ddeebbuuggggiinngg
<<??pphhpp
$$ssttuuffff == aarrrraayy((""nnaammee"" ==>> ""XXYYZZ"",,""ccoouurrssee"" ==>> ""MMCCAA""));;
pprriinntt__rr(($$ssttuuffff));;
??>>
24. your name
your
caption
here
Looping
while loops
through a block of code as long
as the specified condition is true
do...while loops
through a block of code
once, and then repeats the loop as long as the
specified condition is true
for loops
through a block of code a
specified number of times
foreach loops
through a block of code for
each element in an array
25. your name
your
caption
here
Looping Through an associative array
<?php
$stuff = array("name" => "XYZ","course" =>
"MCA");
foreach($stuff as $k => $v ) {
echo "Key=",$k," Val=",$v,"n";
}
?>
KKeeyy==nnaammee VVaall==XXYYZZ
KKeeyy==ccoouurrssee VVaall==MMCCAA
28. your name
your
caption
here
Forms Submit Data
<p>
<h2>Enter username and password</h2>
<form name="input" action="" method="get">
Username: <input type="text" name="usr"><br>
Password: <input type="password" name="pwd">
<input type="submit" value="Submit">
</form>
</P>
29. your name
your
caption
here
$_GET , $_POST & $_REQUEST
PHP loads the values for tthhee UURRLL ppaarraammeetteerrss iinnttoo aann aarrrraayy
ccaalllleedd $$__GGEETT aanndd tthhee PPOOSSTT ppaarraammeetteerrss iinnttoo aann aarrrraayy ccaalllleedd
$$__PPOOSSTT
TThheerree iiss aannootthheerr aarrrraayy ccaalllleedd $$__RREEQQUUEESSTT wwhhiicchh mmeerrggeess GGEETT
aanndd PPOOSSTT ddaattaa
30. your name
your
caption
here
$_GET and $_POST
<p>
<h2>Enter username and password</h2>
<form name="input" method="get">
Username: <input type="text" name="usr" ><br>
Password: <input type="password" name="pwd">
<input type="submit" value="Submit">
</form>
<pre>$_GET:
<?php
print_r($_GET);
?>
</pre>
</P>
31. your name
your
caption
here
Processing POST Data
<?php
$guess = '''';
$message = false;
if ( isset($_POST[''guess'']) ) {
// Trick for integer / numeric parameters
$guess = $_POST[''guess''] + 0;
if ( $guess == 42 ) {
$message = "Great job!";
} else if ( $guess < 42 ) {
$message = "Too low";
} else {
$message = "Too high...";
}
}
?>
32. your name
your
caption
here
Processing POST Data
<html>
<head>
<title>A Guessing game</title>
</head>
<body style="fontfamily:
sansserif;">
<p>Guessing game...</p>
<?php
if ( $message !== false ) {
echo("<p>$message</p>n");
}
?>
<form method="post">
<p><label for="guess">Input Guess</label>
<input type="text" name="guess" id="guess" size="40">
33. your name
your
caption
here
Processing POST Data
<?php echo 'value="' . htmlentities($guess) . ''"'';
?>
/></p>
<input type="submit"/>
</form>
</body>
34. your name
your
caption
here
Other Input Types
TTeexxtt
PPaasssswwoorrdd
RRaaddiioo BBuuttttoonn
CChheecckk BBooxx
SSeelleecctt // DDrroopp--DDoowwnn
TTeexxttAArreeaa
CCoolloorr
DDaattee
EEmmaaiill
NNuummbbeerr
UUrrll
37. your name
your
caption
here
Terminology
DDaattaabbaassee -- CCoonnttaaiinnss mmaannyy ttaabblleess
AAttttrriibbuuttee ((aallssoo ccoolluummnn oorr ffiieelldd)) -- OOnnee ooff ppoossssiibbllyy mmaannyy eelleemmeennttss ooff
ddaattaa ccoorrrreessppoonnddiinngg ttoo tthhee oobbjjeecctt rreepprreesseenntteedd bbyy tthhee rrooww
RReellaattiioonn ((oorr ttaabbllee)) -- CCoonnttaaiinnss ttuupplleess aanndd aattttrriibbuutteess
TTuuppllee ((oorr rrooww)) -- IIss aa sseett ooff ffiieellddss iitt ggeenneerraallllyy rreepprreesseennttss aann ““oobbjjeecctt””
lliikkee aa ppeerrssoonn oorr aa mmuussiicc ttrraacckk
38. your name
your
caption
here
SQL
Structured QQuueerryy LLaanngguuaaggee iiss tthhee llaanngguuaaggee wwee uussee ttoo iissssuuee
ccoommmmaannddss ttoo tthhee ddaattaabbaassee
CCrreeaattee aa ttaabbllee
RReettrriieevvee ssoommee ddaattaa
IInnsseerrtt ddaattaa
DDeelleettee ddaattaa
39. your name
your
caption
here
MySQL
MySQL is a popular choice of database ffoorr uussee iinn wweebb
aapppplliiccaattiioonnss,, aanndd iiss aa cceennttrraall ccoommppoonneenntt ooff tthhee wwiiddeellyy uusseedd
LLAAMMPP ooppeenn ssoouurrccee wweebb aapppplliiccaattiioonn ssooffttwwaarree ssttaacckk ((aanndd ootthheerr
''AAMMPP'' ssttaacckkss)).. FFrreeee--ssooffttwwaarree--ooppeenn ssoouurrccee pprroojjeeccttss tthhaatt rreeqquuiirree aa
ffuullll--ffeeaattuurreedd ddaattaabbaassee mmaannaaggeemmeenntt ssyysstteemm oofftteenn uussee MMyySSQQLL..
40. your name
your
caption
here
Communicating with MySQL
We need some kind of interface ssoo wwee ccaann ttyyppee SSQQLL ccoommmmaannddss
CCoommmmaanndd lliinnee
FFuullll SSccrreeeenn
43. your name
your
caption
here
MySQL Commands
SShhooww ddaattaabbaasseess
CCrreeaattiinngg aa ddaattaabbaassee
CCrreeaattiinngg aa ttaabbllee
DDeessccrriibbee ttaabbllee
44. your name
your
caption
here
MySQL Commands
CCrreeaattiinngg aa DDaattaabbaassee
CREATE DATABASE stddatabase;
GRANT ALL ON stddatabase.* TO root@localhost
IDENTIFIED BY ''admin'';
USE stddatabase;
45. your name
your
caption
here
CCrreeaattiinngg aa TTaabbllee
create table usrinfo(
fname varchar(32) not null,
lname varchar(32),
uname varchar(32) not null,
pwd varchar(32) not null,
sex varchar(6) not null,
deg varchar(6) not null,
bname int,
iname int,
about varchar(300),
tech varchar(10),
color varchar(10),
bday date,
email varchar(32),
mno int,
url varchar(32),
primary key (uname)
);
46. your name
your
caption
here
Describe Table
Describe usrinfo;
47. your name
your
caption
here
Inserting into Table
INSERT INTO USRINFO(fname, lname, uname, pwd,
sex, deg, bname, iname, about, tech, color,
bday, mno, url)
VALUES(''ramesh'',''wadawadagi'',''rswbec'',''admin''
,''male'',''mtech'',1,3,''i love to work on php
and
mysql'',''html'',''#0000ff'',09/02/2000,''809507054
8'',''rswlib.co.in'');
48. your name
your
caption
here
Inserting into Table
INSERT INTO USRINFO(fname, lname, uname, pwd, sex, deg,
bname, iname, about, tech, color, bday, mno, url)
VALUES(''ramesh'',''wadawadagi'',''rswbec'',''admin'',''male'',''mtech'',
1,3,''i love to work on php and
mysql'',''html'',''#0000ff'',09/02/2000,''8095070548'',''rswlib.co.in
'');
FFeettcchhiinngg ffrroomm tthhee TTaabbllee
SELECT * FROM usrinfo;
49. your name
your
caption
here
Accessing MySQL Using PDO
Multiple Ways ttoo AAcccceessss MMyySSqqll
PPHHPP iiss eevvoollvviinngg -- tthheerree aarree tthhrreeee wwaayyss ttoo aacccceessss
MMyySSqqll ffrroomm PPHHPP
LLeeggaaccyy nnoonn--OOOO mmyyssqqll__ rroouuttiinneess ((ddeepprreeccaatteedd))
NNeeww mmyyssqqllii ((OOOO vveerrssiioonn tthhaatt iiss ssiimmiillaarr ttoo mmyyssqqll__))
PPDDOO -- PPoorrttaabbllee DDaattaa OObbjjeeccttss
50. your name
your
caption
here
Accessing MySQL Using different ways
53. your name
your
caption
here
Database Connection using PDO
The PHP Data Objects (PDO) extension defines a lightweight, consistent
interface for accessing databases in PHP.
PDO::exec —— Execute an SQL statement and return the number of affected
rows
PDO::prepare —— Prepares a statement for execution and returns a
statement object
PDO::query —— Executes an SQL statement, returning a result set as a
PDOStatement object
PDOStatement::execute —— Executes a prepared statement
PDOStatement::fetch —— Fetches the next row from a result set
PDOStatement::fetchAll —— Returns an array containing all of the result
set rows
54. your name
your
caption
here
Database Connection
<?php
try {
$db = new PDO(''mysql:host=localhost;port=8889;dbname=usrdatabase'',''root'',
''admin'');
echo "PDO connection object created";
}
catch(PDOException $e)
{
echo $e>
getMessage();
}
?>
55. your name
your
caption
here
Insert Query
<?php
Try {
$db = new
PDO(''mysql:host=localhost;port=8889;dbname=registration'',''root'',''admin'');
if (isset($_POST[''fname''])
&& isset($_POST[''lname''])
.
.
&& isset($_POST[''about'']))
{
$sql = "INSERT INTO usrinfo (fname,...,about) VALUES (:fname, ..., :about)";
$stmt = $db>
prepare($sql);
$stmt>
execute(array('':fname'' => $_POST[''fname''],
.
.
'':about'' => $_POST[''about''],
));
}
} catch(PDOException $e)
{ echo $e>
getMessage(); }
?>
56. your name
your
caption
here
Select Query
<?php
echo "<pre>n";
$pdo=new PDO(''mysql:host=localhost;port=8889;dbname=usrdatabase'',''root'',
''admin'');
$stmt = $pdo>
query("SELECT * FROM usrinfo");
while ( $row = $stmt>
fetch(PDO::FETCH_ASSOC) ) {
print_r($row);
}
echo "</pre>n";
?>
57. your name
your
caption
here
Select Query
Array
(
[fname] => ramesh
[lname] => wadawadagi
[uname] => rswbec
[pwd] => admin
[sex] => male
[deg] => mtech
[bname] => mtech
[iname] => bec,bagalkot
[about] => i love building apps in php and mysql
[tech] => html
[color] => #0000ff
[bday] => 19810619
[email] => rswbec@gmail
[mno] => 2147483647
[url] => rswlib.co.in
)
58. your name
your
caption
here
Delete Query
<?php
$a=$_GET[''action''];
$db = new
PDO(''mysql:host=localhost;port=8889;dbname=registration'',''root'',''admin'');
$sql="DELETE FROM usrinfo WHERE uname = :uname";
$query = $db>
prepare($sql);
$query>
execute(array('':uname'' => $a));
?>