1 / 17

PHP & MySQL

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

Download Presentation

PHP & MySQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP & MySQL MahakArora VivekBangera

  2. 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

  3. 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

  4. 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!"); ?>

  5. 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!"; } ?>

  6. 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; }

  7. 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);?>

  8. 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

  9. 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>

  10. 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!

  11. 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

  12. 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

  13. 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.

  14. 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();  }?>

  15. 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

  16. 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]'";

  17. Thank You

More Related