PHP&MYSQL Programming

Download Report

Transcript PHP&MYSQL Programming

PHP & MYSQL Programming Lafin Hari Prayudhi 200893279

Introduction

• • • •

PHP

– is a scripting language originally designed for producing Dynamic Web pages. It has evolved to include a command line interface capability – Standalone Graphical Applications PHP is a widely-used general-purpose scripting language that is especially suited for web developm ent and can be embedded into HTML Generally runs on a Web Server; – Taking PHP code as its input – Creating web pages as output. Can be deployed on most web servers and on almo st every OS and platform free of charge.

PHP 5 Basic Language Introduction

PHP Borrows a bit of its syntax from other languages, • Taking the best feature from other languages.

• Creating an easy –to-use Powerful scripting languages HTML Embedding.

Shell Perl,java

C, C++

PHP

Features

PHP have Features :

 HTTP authentication with PHP           Cookies Sessions Dealing with XForms Handling file uploads  POST method uploads    Error Messages Explained Common Pitfalls Uploading multiple files PUT method support  Using remote files Connection handling Persistent Database Connections Safe Mode  Security and Safe Mode  Functions restricted/disabled by safe mode Using PHP from the command line Free software released under the PHP License.

Release History

Major Version

1.0

2.0

3.0

4.0

Minor Version

1.0.0

Release date

1995-06-08 2.0.0

3.0.0

4.0.0

4.1.0

4.2.0

4.3.0

4.4.0

4.4.8

4.4.9

1996 04-16 1998 06-06 2000 05-22 2001 12-10 2002 04-22 2002 12-27 2005 07-11 2008 01-03 2008 08-07

Note

Officially called "Personal Home Page Tools (PHP Tools)". This is the first use of the name "PHP".

Rasmus Lerdorf Considered by its creator as the "fastest and simplest tool" for creating dynamic web pages.

Development moves from one person to multiple developers. Zeev Suraski and Andi Gutmans rewrite the base for this version.

Added more advanced two-stage parse/execute tag-parsing system called the Zend engine.

Introduced 'superglobals' ($_GET, $_POST, $_SESSION, etc.) Disabled register_globals by default. Data received over the network i s not inserted directly into the global namespace anymore, closing po ssible security holes in applications.

Introduced the CLI, in addition to the CGI.

Added man pages for phpize and php-config scripts.

Several security enhancements and bug fixes. Was to be the end of lif e release for PHP 4. Security updates only until 2008-08-08, if necessa ry.

More security enhancements and bug fixes. The last release of the PH P 4.4 series.

Release History (continue)

Major Version

5.0

6.0

Minor Version

5.0.0

5.1.0

5.2.0

5.2.8

5.2.9

5.3.0

6.0.0

Release date

2004 07-13 2005 11-24 2006 11-02 2008 12-08 Emergent bug fix.

Note

Zend Engine II with a new object model.

Performance improvements with introduction of compiler variables in re-engineered PHP Engine.

Enabled the filter extension by default.

2009 02-26 Second quarter of 2009 No date set Bug and security fixes Namespace support; Late static bindings , Jump label (limited goto ), Native closures , Native PHP archives (phar), Garbage collection improvements, Persistent connections with mysqli, sqlite3, file info as a replacement for mime_magic for better MIME support, Ternary shortcut and Internationalization extension Unicode support; removal of ereg extension, 'register_globals', 'ma gic_quotes' and 'safe_mode'; Alternative PHP Cache Old Release; not supported Old Release; still supported Current Release Future Release

How The Web Server Processes PHP files

PHP opening tag ()

Example PHP program

Hello World program

echo "

Hello World!, testing PHP program at database class " ?>

Result of hello.php

Variables

• • • A Variables in PHP don’t need to; – Declare variables before using them.

– Declare type.

A variable can change the type of its value as much as we want.

Variable in PHP preceded with: $

Example Variable expression

Data type integer, floating point, string, etc, have same declaration: – – $PI = 3.14

; $radius = 5 ; – $name = “John” ;

Example Math1.php

Variables Expression

$PI = 3.14

; $radius = 5 ; $circumference=$PI* 2 *$radius; echo "

Circumference with Radius = $radius is $circumference" ; ?>

Result of math1.php

Superglobals

• PHP does not support global variables. However, certain special internal variables behave like global variables similar to other languages. These variables are called

superglobals

. Some examples are ;  $_GET[] . An array that includes all the GET variables that PHP received from the client browser.

 $_POST[]. An array that includes all the POST variables that PHP received from the client browser.

 $_COOKIE[]. An array that includes all the cookies that PHP received from the client browser.

 $_ENV[]. An array with the environment variables.

 $_SERVER[]. An array with the values of the web-server variables.

Comments in PHP

• We can write comments three different ways:  C way /* This is a C like comment * which can span multiple * lines until the closing tags */  C++ way // This is a C++ like comment which ends at the end of the line  Shell way # This is a shell like comment which ends at the end of the line

PHP/MYSQL Function

• • Built-in PHP functions to interact with MySQL. These functions :  Connect to the MySQL server,  Select the correct database,  Send SQL queries,  Perform other communication with MySQL databases.

You only need to know how to use the functions.

Making a Connection

• The first step in communicating with your MySQL database is connecting to the MySQL server, need; – The Host, where the database is located.

– – The User, name of MySQL Password.

account.

$connection= mysql_connect (“ Host ”,” User ”,” password ”) or die (“ message ”);

Example connect.php

Connection to MySQL Example

$host="localhost"; $user=@$_GET['user']; $password="secret2"; echo "user $user is currently trying to connect to database
"; /* Section that executes query */ if(!$connect = mysql_connect($host,$user,$password)) { $message=mysql_error(); echo "$message
"; die(); { } else echo die(); } ?> " Connection MySQL Server Success
"; mysql_close($connect);

Result Connect.php

Selecting the right database

• Use the mysql_select_db follows: function as $db = mysql_select_db (“ databasename ”,$ connectionname ) or die (“ message ”);

Select database example

Select Database Example

$host = "localhost"; $user = "Lafin"; $password = "secret2"; /* Section that executes query */ if(!$connect = mysql_connect($host,$user,$password)) { $message=mysql_error(); echo "$message
"; die(); } else { echo " Connection MySQL Server Success
"; $database="test2"; $db=mysql_select_db($database,$connect) or die ("Couldn't select database."); mysql_close($connect); } ?>

Result select_database.php

Sending SQL queries

• • The

query

is a request to the MySQL server to store some data, update some data, or retrieve some data.

Put the SQL query function $query = “ into a variable and send it to the MySQL server by using the mysql_query: SELECT * FROM employee ”; $result = mysql_query ($query) or die (“ Couldn’t execute query .”);

Getting one row of data

• • To move the data from its temporary location and put it into variables that we can use in our program, we use the PHP function mysql_fetch_array .

$row = mysql_fetch_array ($result,typeofarray);

Example code for send queries and take raw of data

Sending SQL Query Example

$user="Lafin“; $host="localhost“; $password="secret2“; $database = "test2"; $connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server"); $db = mysql_select_db($database,$connection) or die ("Couldn't select database"); $Fname = "John"; //First name was typed in a form by user $query = "SELECT * FROM employee WHERE Fname='$Fname'"; $result = mysql_query($query) or die ("Couldn't execute query."); /* Display results in a table */ echo "

$Fname

"; echo ""; echo ""; while ($row = mysql_fetch_array($result)) { extract($row); $Serial = number_format($SSN,0); echo "\n \n \n \n \n \n"; echo "\n"; } echo "

$Fname$MINIT$LNAME $SSN

\n"; ?>

Result Queries & Raw data

Getting information from the user

• Many applications are designed to ask questions that users answer by typing information. Sometimes the information is stored in a database, or is used in conditional statements to deliver an individual Web page. Some of the most common application tasks that require users to answer questions are –

Online ordering

Registering

– –

Logging in Viewing selected information

Conclusion

 MySQL and PHP have become the “bread and butter” of web dynamic application builders.

 It is the combination are most likely to encounter today and probably for the years to come.

 Can be deployed on most web servers an d on almost every OS and platform free o f charge.

Thank You