PHP Introduction ( Fedora )
-
Upload
kishore-kumar -
Category
Technology
-
view
4.193 -
download
0
Transcript of PHP Introduction ( Fedora )
PHP Tutorial
ByMr. V. Kishore Kumar MCA (2009-12)
Under the guidance ofMr. S. Amirtharaj M.E., &Mr. V. Neethidevan M.E.,
Contents
• Introduction to PHP
• Deploying PHP files
• PHP Functions to interact with Mysql
05/03/23 2MCA, Mepco Schlenk Engineering College, Sivakasi
Introduction to PHP
Contents05/03/23 3MCA, Mepco Schlenk Engineering College, Sivakasi
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
05/03/23 4MCA, Mepco Schlenk Engineering College, Sivakasi
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"
05/03/23 5MCA, Mepco Schlenk Engineering College, Sivakasi
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
05/03/23 6MCA, Mepco Schlenk Engineering College, Sivakasi
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 PHPWAMP – Windows Apache Mysql & PHPLAMP – Linux Apache Mysql & PHP
05/03/23 7MCA, Mepco Schlenk Engineering College, Sivakasi
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.
05/03/23 8MCA, Mepco Schlenk Engineering College, Sivakasi
Sample file<html><body>
<?phpecho "Hello World";?>
</body></html>
Echo – output text in PHP.
There are two basic statements to output text with PHP: echo and print
05/03/23 9MCA, Mepco Schlenk Engineering College, Sivakasi
Comments in PHP
<html><body>
<?php//This is a comment
/*This isa commentblock*/?>
</body></html>
In PHP, we use // to make a one-line comment or /* and */ to make a comment block:
05/03/23 10MCA, Mepco Schlenk Engineering College, Sivakasi
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)
05/03/23 11MCA, Mepco Schlenk Engineering College, Sivakasi
Creating or Declaring PHP Variables• PHP has no command for declaring a variable
• PHP is a loosely typed language, automatically converts the variable to the correct data type, depending on its valueNote: When you assign a text value to a variable, put quotes around the value
<?php$txt="Hello World!";$x=16;?>
05/03/23 12MCA, Mepco Schlenk Engineering College, Sivakasi
The Concatenation Operator
• The concatenation operator (.) is used to put two string values together
• The output of the code above will be:
Note : Operators, if..else, switch, loops & etc. are all same as of C syntax.
<?php$txt1="Hello World!";$txt2="What a nice day!";echo $txt1 . " " . $txt2;?>
Hello World! What a nice day!
05/03/23 13MCA, Mepco Schlenk Engineering College, Sivakasi
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
• 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":
<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>
05/03/23 14MCA, Mepco Schlenk Engineering College, Sivakasi
“welcome.php”
Output
• Note: $_POST[“ “] specifies how the input value passed from another form or page is get into new PHP scripting page
<html><body>
Welcome <?php echo $_POST["fname"]; ?>!<br />You are <?php echo $_POST["age"]; ?> years old.
</body></html>
Welcome John!You are 28 years old.
05/03/23 15MCA, Mepco Schlenk Engineering College, Sivakasi
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.
05/03/23 16MCA, Mepco Schlenk Engineering College, Sivakasi
Deploying PHP files
Contents05/03/23 17MCA, Mepco Schlenk Engineering College, Sivakasi
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:\wamp\www”
• For further instructions please refer http://www.tinkertech.net/tutor/wamp/index.html
05/03/23 18MCA, Mepco Schlenk Engineering College, Sivakasi
Fedora
• Save your php files into “\var\www\html\” folder. To place your files first we have to set access permissions.
Follow the given steps below1. Open the Terminal.2. Log in as Super User. Type “su” command it
will ask for root password ( ask the technicians or respective faculties).
05/03/23 19MCA, Mepco Schlenk Engineering College, Sivakasi
Follow the steps (contd..)3.Next step is to change our current working
location.4.Type “cd ..” as many times until your present
working directory(can be found by using pwd command) reaches “/”.
5.Type cd var/www.6.Type “ls” it will list all the files & directories
present in that directory. We have to deploy our php files under HTML.
05/03/23 20MCA, Mepco Schlenk Engineering College, Sivakasi
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.
05/03/23 21MCA, Mepco Schlenk Engineering College, Sivakasi
• (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.
05/03/23 22MCA, Mepco Schlenk Engineering College, Sivakasi
• 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
05/03/23 23MCA, Mepco Schlenk Engineering College, Sivakasi
PHP Functions to interact with Mysql
Contents05/03/23 24MCA, Mepco Schlenk Engineering College, Sivakasi
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
05/03/23 25MCA, Mepco Schlenk Engineering College, Sivakasi
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.
05/03/23 26MCA, Mepco Schlenk Engineering College, Sivakasi
PHP Functions to access MySqlBefore 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,password);
05/03/23 27MCA, Mepco Schlenk Engineering College, Sivakasi
Ex :-)<?php$con = mysql_connect("localhost","root","");if (!$con){die('Could not connect: ' . mysql_error());}// some codemysql_close($con);?>05/03/23 28MCA, Mepco Schlenk Engineering College, Sivakasi
• 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.
05/03/23 29MCA, Mepco Schlenk Engineering College, Sivakasi
• 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.
05/03/23 30MCA, Mepco Schlenk Engineering College, Sivakasi
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']).
05/03/23 31MCA, Mepco Schlenk Engineering College, Sivakasi
05/03/23 32MCA, Mepco Schlenk Engineering College, Sivakasi