170 likes | 301 Views
PHP & MySQL. Mahak Arora Vivek Bangera. Outline. How PHP works Basic scripting in PHP Forms in PHP(GET & POST Variables) SQL basics PHP and MySQL connection How to query database using PHP SESSION Management. How PHP works. It is Server Side Scripting
E N D
PHP & MySQL MahakArora VivekBangera
Outline • How PHP works • Basic scripting in PHP • Forms in PHP(GET & POST Variables) • SQL basics • PHP and MySQL connection • How to query database using PHP • SESSION Management
How PHP works • It is Server Side Scripting • PHP files can contain text, HTML, JavaScript code, and PHP code • PHP script is stored on server. • The client will not get to see the actual script but only the output of the script
Basic Scripting in PHP <?php $txt="Hello World!"; echo $txt;?> • <?php • $txt1="Hello world!"; • $txt2="What a nice day!"; • echo $txt1 . " " . $txt2;?> <?php echo strlen("Hello world!"); ?>
Conditional Statements Switch If-Else <?php $t=date("H"); if ($t<"20") {echo "Have a good day!“;} Else {echo "Have a good night!"; } ?> <?php $favcolor="red"; switch ($favcolor) { case "red": echo "Your favoritecolor is red!“; break; case "blue": echo "Your favoritecolor is blue!"; break; case "green": echo "Your favoritecolor is green!"; break; default: echo "Your favoritecolor is neither red, blue, or green!"; } ?>
Loops While Loop <?php $i=1; while($i <= 5) { echo "The number is " . $i . "<br>"; $i++; } ?> For-each Loop Syntax of For Loop <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br>“; } ?> for (init; condition; increment) { code to be executed; }
PHP Arrays <?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> <?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");echo "Peter is " . $age['Peter'] . " years old.";?> <?php $numbers=array(4,6,2,22,11); sort($numbers);?>
Functions Without Parameter $fname as a parameter <?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> <?php function writeName($fname) { echo $fname . " Refsnes.<br>"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is “; writeName("Hege"); echo "My brother's name is “; writeName("Stale"); ?> Function call
Forms using POST Index.php welcome.php <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="fname"> Age: <input type="text" name="age"> <input type="submit"> </form> </body> </html> <html> <body> Welcome <?php echo $_POST["fname"]; ?>! <br> You are <?php echo $_POST["age"]; ?> years old. </body> </html>
Forms using GET http://localhost/welcome.php?fname=Peter&age=37 <form action="welcome.php" method="get“> Name: <input type="text" name="fname"> Age: <input type="text" name="age"> <input type="submit"> </form> Welcome <?php echo $_GET["fname"]; ?>. <br> You are <?php echo $_GET["age"]; ?> years old!
SQL Basics Queries DDL(Data Definition Language) DML(Data Manipulation Language) SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
Basic SQL Queries CREATE DATABASE test; CREATE TABLE `students` ( `id` int(11) AUTO_INCREMENT, `name` varchar(40), `email_id` varchar(30), `dob` date, `password` varchar(40), `fruit` varchar(10), PRIMARY KEY (`id`) ); INSERT INTO students (name, email_id, dob, password) VALUES ('your_name' ,' your_email@id.com ' , '1992-11-13' , md5('your_password')); SELECT * FROM students; SELECT password FROM students WHERE email_id=“your_email@id.com”; SELECT fruit, COUNT( * ) FROM students GROUP BY fruit
PHP and MySQL • MySQL is an open source database server • PHP has native bindings for MySQL. • For using PHP with other Database Servers you need to import appropriate bindings and wrappers.
PHP-MySQL connection <?php$con=mysqli_connect(“localhost",“test",”test");$sql="CREATE DATABASE test";if (mysqli_query($con,$sql)) { echo "Database test created successfully“; }else { echo "Error creating database: " . mysqli_error(); }?>
Session Variables in PHP A PHP session variable is used to store for a user session that is available for all pages. POST& GET POST& GET Index.php Registered.php Store.php SESSION VARIABLES
How to use Session variables <?php session_start(); if(md5($password)==$db_pass['password']) { $_SESSION['loginid']=$_POST['email_id']; } ?> $sql="UPDATE students SET fruit='$_POST[fruit]' where email_id='$_SESSION[loginid]'";