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
Perl,java
Shell
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.
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
Minor
Version Version
Release
date
Note
1.0
1.0.0
1995-06-08
Officially called "Personal Home Page Tools (PHP Tools)".
This is the first use of the name "PHP". Rasmus Lerdorf
2.0
2.0.0
1996-04-16
Considered by its creator as the "fastest and simplest tool" for
creating dynamic web pages.
3.0
3.0.0
1998-06-06
Development moves from one person to multiple developers. Zeev
Suraski and Andi Gutmans rewrite the base for this version.
4.0.0
2000-05-22
Added more advanced two-stage parse/execute tag-parsing system
called the Zend engine.
4.1.0
2001-12-10
Introduced 'superglobals' ($_GET, $_POST, $_SESSION, etc.)
4.2.0
2002-04-22
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.
4.3.0
2002-12-27
Introduced the CLI, in addition to the CGI.
4.4.0
2005-07-11
Added man pages for phpize and php-config scripts.
4.4.8
2008-01-03
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.
4.4.9
2008-08-07
More security enhancements and bug fixes. The last release of the PH
P 4.4 series.
4.0
Release History (continue)
Major
Minor
Version Version
5.0
6.0
Release
date
Note
5.0.0
2004-07-13
Zend Engine II with a new object model.
5.1.0
2005-11-24
Performance improvements with introduction of compiler variables
in re-engineered PHP Engine.
5.2.0
2006-11-02
Enabled the filter extension by default.
5.2.8
2008-12-08
Emergent bug fix.
5.2.9
2009-02-26
Bug and security fixes
5.3.0
Second quarter
of 2009
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
6.0.0
No date set
Unicode support; removal of ereg extension, 'register_globals', 'ma
gic_quotes' and 'safe_mode'; Alternative PHP Cache
Old Release; not supported
Current Release
Old Release; still supported
Future Release
How The Web Server Processes
PHP files
PHP
opening tag
(<?)
PHP File
(*.php)
Scanning in
Switches
HTML
into PHP
Mode
mode
Send Out
put to Br
owser
Only if any output
Return to
HTML mode
PHP
closing tag
(?>)
Example PHP program
<html>
<head><title>Hello World program</title></head>
<body>
<?php
echo "<p>Hello World!, testing PHP program at
database class "
?>
</body>
</html>
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
<html>
<head><title> Variables Expression</title></head>
<body>
<?php
$PI=3.14;
$radius=5;
$circumference=$PI*2*$radius;
echo "<p> Circumference with Radius = $radius is
$circumference";
?>
</body>
</html>
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 account.
– Password.
$connection=mysql_connect(“Host”,”User”,”password”)
or
die (“message”);
Example connect.php
<html>
<head><title>Connection to MySQL Example</title></head>
<body>
<?php
$host="localhost";
$user=@$_GET['user'];
$password="secret2";
echo "user $user is currently trying to connect to database<br>";
/* Section that executes query */
if(!$connect = mysql_connect($host,$user,$password))
{
$message=mysql_error();
echo "$message<br>";
die();
}
else
{
echo " Connection MySQL Server Success <br>";
mysql_close($connect);
die();
}
?>
</body></html>
Result Connect.php
Selecting the right database
• Use the mysql_select_db function as
follows:
$db = mysql_select_db(“databasename”,$connectionname)
or
die (“message”);
Select database example
<html>
<head><title>Select Database Example</title></head>
<body>
<?php
$host="localhost";
$user="Lafin";
$password="secret2";
/* Section that executes query */
if(!$connect = mysql_connect($host,$user,$password))
{
$message=mysql_error();
echo "$message<br>";
die();
}
else
{
echo " Connection MySQL Server Success <br>";
$database="test2";
$db=mysql_select_db($database,$connect) or die ("Couldn't select database.");
mysql_close($connect);
}
?>
</body></html>
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 into a variable and send
it to the MySQL server by using the
function mysql_query:
$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
<html>
<head><title>Sending SQL Query Example</title></head>
<body>
<?php
$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 "<h1>$Fname</h1>";
echo "<table cellspacing='15'>";
echo "<tr><td colspan='3'><hr></td></tr>";
while ($row = mysql_fetch_array($result))
{ extract($row);
$Serial = number_format($SSN,0);
echo "<tr>\n <td>$Fname</td>\n <td>$MINIT</td>\n <td>$LNAME</td>\n <td align='right'>
$SSN</td>\n </tr>\n";
echo "<tr><td colspan='3'><hr></td></tr>\n";
}
echo "</table>\n";
?>
</body></html>
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