4. PHP
• PHP stands for PHP: Hypertext Preprocessor
• PHP is a powerful tool for making dynamic and
interactive Web pages.
• PHP is a server-side scripting language, like ASP
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic
ODBC, etc.)
• PHP is an open source software
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 4
5. What is a PHP file ?
• PHP files can contain text, HTML tags and
scripts
• PHP files are returned to the browser as plain
HTML
• PHP files have a file extension of ".php",
".php3", or ".phtml"
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 5
6. Why PHP ?
• PHP runs on different platforms (Windows,
Linux, Unix, etc.)
• PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
• PHP is FREE to download from the official PHP
resource: www.php.net
• PHP is easy to learn and runs efficiently on the
server side
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 6
7. Where to Start ?
To get access to a web server with PHP support,
you can:
• Install Apache (or IIS) on your own server,
install PHP, and MySQL
• Or install WAMP s/w for windows, LAMP for
Linux to easily develop web applications in
PHP
WAMP – Windows Apache Mysql & PHP
LAMP – Linux Apache Mysql & PHP
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 7
8. PHP Basics
• The PHP script is executed on the server, and the plain
HTML result is sent back to the browser
• A PHP script always starts with <?php and ends with ?>
• A PHP script can be placed anywhere in the document
• A PHP file normally contains HTML tags, and some PHP
scripting code
• Each code line in PHP must end with a semicolon.
• The semicolon is a separator and is used to distinguish
one set of instructions from another.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 8
9. Sample file
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Echo – output text in PHP.
There are two basic statements to output text with PHP: echo and print
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 9
10. Comments in PHP
In PHP, we use // to make a one-line comment or /* and */ to make a comment
block:
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 10
11. PHP Variables
• Variables in PHP starts with a $ sign, followed by the
name of the variable
• The variable name must begin with a letter or the
underscore character
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• A variable name should not contain spaces
• Variable names are case sensitive (y and Y are two
different variables)
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 11
12. Creating or Declaring PHP
Variables
• PHP has no command for declaring a variable
<?php
$txt="Hello World!";
$x=16;
?>
• PHP is a loosely typed language, automatically converts
the variable to the correct data type, depending on its
value
Note: When you assign a text value to a variable, put
quotes around the value
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 12
13. The Concatenation Operator
• The concatenation operator (.) is used to put two
string values together
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
• The output of the code above will be:
Hello World! What a nice day!
Note : Operators, if..else, switch, loops & etc. are all same as of C syntax.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 13
14. PHP Forms
• The most important thing to notice when dealing with HTML
forms and PHP is that any form element in an HTML page
will automatically be available to your PHP scripts
<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>
• When a user fills out the form above and clicks on the submit
button, the form data is sent to a PHP file, called
"welcome.php":
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 14
15. “welcome.php”
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Output
Welcome John!
You are 28 years old.
• Note: $_POST[“ “] specifies how the input value passed from another
form or page is get into new PHP scripting page
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 15
16. PHP $_GET Function
• Collect values in a form with method=“get”
• While using this method all values are displayed in
URL.
PHP $_POST Function
• Collect values in a form with method=“post”
• While using this method all values are not displayed
(invisible to others) in URL.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 16
18. Windows
• Install WAMP Web-Server Software and
deploy all your applications.
• Consider you have installed WAMP on “C:”
Drive. Then place all your .php files in the
following folder “C:wampwww”
• For further instructions please refer
http://www.tinkertech.net/tutor/wamp/index.html
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 18
19. Fedora
• Save your php files into “varwwwhtml”
folder. To place your files first we have to set
access permissions.
Follow the given steps below
• Open the Terminal.
• Log in as Super User. Type “su” command it
will ask for root password ( ask the technicians or
respective faculties).
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 19
20. Follow the steps (contd..)
• Next step is to change our current working
location.
• Type “cd ..” as many times until your present
working directory(can be found by using pwd
command) reaches “/”.
• Type cd var/www.
• Type “ls” it will list all the files & directories
present in that directory. We have to deploy
our php files under HTML.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 20
21. Follow the steps (contd..)
To set access permission to HTML folder use
chmod command
type chmod 777 html.
Now you have set the access permissions to
that folder. We can paste any files in under that
folder.
To display our PHP files in Browser,
• Before opening browser, ensure that apache
services is running or not.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 21
22. • (In Browser) Type “localhost” in url and press
enter. If the apache services is running
correctly, it will shows fedora test page as
shown below.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 22
23. • If it shows “Problems on page load error”
means, the apache services is not running.
open terminal (enter as super user (“su”))
– Type apchectl start – to start the apache server.
– Type apchectl stop – to stop the apache server.
(its not mandatory to stop).
• Note : create separate folders for your php
project under HTML folder
Ex: if folder name is “test”, then in url type as
localhost/test/yourphpfilename along with .php extension
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 23
24. PHP Functions to interact with
Mysql
Contents
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 24
25. MySql
• Most popular open-source database system
• We can create any number of databases under
particular databases we can create any number
of tables.
• Mysql GUI has been available in the market,
which allows us to add, edit, modify records,
tabels, databases easily
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 25
26. Steps to be followed in Fedora environment to
use Mysql
• Log in as super user in terminal by using “su”
command
• Then type mysql
• To lists all existing databases, type show
databases;
• To select a database, type use database_name;
• to lists all tables exists under that database, type
show tables;
• Note: Tables can be created only by selecting a
database first.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 26
27. PHP Functions to access MySql
Before you can access data in a database,
you must create a connection to the database.
In PHP, this is done with the mysql_connect()
function.
mysql_connect(servername,username,passwo
rd);
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 27
28. Ex :-)
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 28
29. • The connection will be closed automatically
when the script ends. To close the connection
before, use the mysql_close() function.
• mysql_select_db() -> Sets the current active
database on the server that's associated with
the specified link identifier. Every subsequent
call to mysql_query() will be made on the
active database.
• mysql_query() -> executes the INSERT
INTO statement, and a new record will be
added to the table.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 29
30. • mysql_num_rows() -> Retrieves the number
of rows from a result set. This command is
only valid for statements like SELECT or
SHOW that return an actual result set. To
retrieve the number of rows affected by a
INSERT, UPDATE, REPLACE or DELETE
query, use mysql_affected_rows().
• mysql_fetch_array() -> return the first row
from the recordset as an array. Each call to
mysql_fetch_array() returns the next row in
the recordset.
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 30
31. Sample program to display data from database :
Ex:
mysql_connect("localhost","root","");
mysql_select_db("my_db");
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
The while loop loops through all the records in the
recordset. To print the value of each row, we use the
PHP $row variable ($row['FirstName'] and
$row['LastName']).
06/07/12 MCA, Mepco Schlenk Engineering College, Sivakasi 31