PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Using Macros and Visual Basic for Applications (VBA) with Excel
Query Manager. QM is a collection of tools you can use to obtain information from the AS/400 database Used to –select, arrange, and analyze information.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Dynamic Web site With PHP and MySQL. MySQL The combination of MySQL database and PHP scripting language is optimum for building dynamic websites. MySQL.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
CSCI 6962: Server-side Design and Programming
Session 5: Working with MySQL iNET Academy Open Source Web Development.
SQL Maestro Hello World IQ Associates. Contents Initial setup Hello World.
INTERNET APPLICATION DEVELOPMENT For More visit:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Installing the SAFARIODBC.EXE For use with Excel May 3, 2002.
Concepts of Database Management Seventh Edition
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Website Development with PHP and MySQL Saving Data.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Database control Introduction. The Database control is a tool that used by the database administrator to control the database. To enter to Database control.
Present :Arezoo Mollahasani. Step 1  Define your server connection Open MySQL WorkBench and click New Server Instance on the right of the window.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
Creating PHPs to Insert, Update, and Delete Data CS 320.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Unit 5 Microsoft SQL Server and MySQL. Key Concepts DBMS variations SQL Server features SQL Server Management Studio MySQL features Scripts Queries Database.
Storing and Retrieving Data
Introduction to PHP.
PHP Programming.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Presentation On How To Create Connection To A Database.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
Unit 1 – Web Concepts Instructor: Brent Presley.
Chap 2 – Getting Started COMP YL Professor Mattos.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
ASP.NET Programming with C# and SQL Server First Edition
, MS-Access, QBE, Access/Oracle
Introduction to Dynamic Web Programming
BASIC PHP and MYSQL Edward S. Flores.
PHP Introduction.
Aqua Data Studio.
Tutorial 6 PHP & MySQL Li Xu
Introduction to Web programming
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Presentation transcript:

PHP Programming

Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling (MySQL, MSSQL, ODBC)

Background Personal Home Page – C kind of scripts written in Perl language, by Rasmus Lerdorf in He called the language as PHP/FI – Personal Home Page / Forms Interpreter Version 2.0 was released Then came 3.0, 4.0. They were called PHP simply Recent version is PHP 5.2.3

Installation Apache server need to be installed first The config file for apache server will be present in the conf folder under the apache installed directory Set the DocumentRoot to the path where the PHP files will be stored Install PHP. Point apache conf directory when it asks so and select the appropriate web server. Move the PHP.ini file to C:\WNDOWS directory

PHP Comments All php files are saved with extension.php The can be written in notepad or any text editor Single line comment –// this is single line comment Multi line comment –/*…. This is a multi line comment */

Sample PHP The above program will display hello in the browser

Variables Variables start with a $ symbol Variables can contain _ or numbers or alphabets $ should be followed by _ or alphabet and not by a number PHP is loosely typed language. There is no strict data typing Variable can be assigned with any values

Conditions If else if(condn) {….} elseif(condn) {….} else { ….} Switch case switch(var) { case c1: statements;break. Default: statements; break; }

Loops For for(intialisation;condition;increment/decrement) { statements } While while(condn) { ….} Do While do {….} while(condn);

Functions and Parameters PHP functions need to be defined with key word function It can have zero or more values (parameters) Functions may or may not return values If a function need to return value, the last statement of the function should be return –return value;

Functions Parameter less function <?php function sayHi() { echo “hi”; } ?> This can be called as in the program

Functions Parameterized function <?php function greet($name) { echo “Hello “. $name; } ?> This can be called This gives an output Hello Ram

Functions Function returning value <?php function add($a,$b) { return ($a + $b); } ?> When called like we will get an output 3 in the browser.

File Handling This involves 5 tasks –Opening a file –Reading data from a file –Displaying the read data –Writing contents to another file –Closing a file

Opening a file $fp = fopen(‘filename’,’mode’); Eg $fp = fopen(‘c:\abc.txt’,’r’); –This opens a file abc.txt in read only mode Available modes: –r – read only –w – write only –w+ - read write –A – append – adding to the end

Reading a file Several methods are available –fread(filepointer,no of bytes to read) –fgetc(filepointer) – Reads character by character –fgets(filepointer) – Reads line by line The read content can be stored in a variable $data = fread($fp,10) – this reads 10 characters from file pointed by file pointer $fp and stores in $data If we want to read characters till end, we need to use a loop with condition checking for End of File

Writing to file We can use echo $data, to print the contents read from the file to browser Or we can open another file in write mode and put the contents to that file using either of these methods –fwrite(filepoiner,data); –fputc(filepointer,char); - writes character by character –fputs(filepointer,line); - writes line by line Eg - fwrite($fpw,$data);

Closing a file feof(fp) – Checks for end of file. Returns –1 if EOF is reached. Otherwise returns 0 To close a file use fclose(filepointer) method Eg. fclose($fp); –This closes the file pointed by $fp.

Database Handling PHP can connect to –MySQL –MSSQL –Access and other databases like oracle, postgre sql etc There are separate methods available for connecting to the databases

MySQL and MSSQL Connection mysql_connect(dbserver,userid,password) mssql_connect(dbserver,userid,password) –These methods are used for connecting to MySQL and MSSQL server using a userid and password $con = mysql_connect(‘localhost’,’root’,’root’); –This gets a connection to the local mysql server using the credentials root and root –If server cannot be connected, it will throw an error stating the problem Note: Mysql and MSSQL can be accessed in identical way except for the preceeding mysql or mssql. So will use mysql henceforth.

MySQL Select DB mysql_select_db(connection,dbname) mysql_select_db($con,”test”); –This will select the db test under the server localhost –If unable to select the database, an error will be thrown

MySQL Execute Query mysql_query(connection,sql statement); This will execute the sql statement on the database and store the result in a variable Eg –$rs = mysql_query($con,select stmt); –The rows of select statement will be stored in $rs –$row = mysql_fetch_array($rs); This will fetch a row and store in $row Values can be accessed like - $row[“ID”] – returns value of column ID in the fetched row.

MySQL and MSSQL Close To close a db connection we have close method –mysql_close(connection); –mssql_close(connection); Example –mysql_close($con); –mssql_close($con);

ODBC Data handling Connect to a data source –odbc_connect(dsn,uname,pwd); –DSN – Data Source Name –Go to Control Panel -> Administrative Tools -> Datasources (ODBC) –Click on ODBC and select System DSN tab. –Click Add and choose Access Database (mdb) and click Finish –In the dialog that appears, give DSN Name and Description

ODBC Data handling Contd.. –Click on Select under Database section –Choose a mdb (access database file) and give OK $conn=odbc_connect('nwind','',''); –Here nwind is the DSN that we have created in our system to access a database –We have not given any user name or password during DSN creation –So those fields are left empty.

Selecting rows from a table $rs = odbc_exec($conn,$sql); –$conn – connection string –$sql – SQL query to select rows from table –$rs is the result set of the query execution odbc_fetch_row($rs) –This command fetches row by row from the result set $cid = odbc_result($rs,"CustomerID"); –This command fetches value for the column CustomerID from the current row and stores it in the variable $cid.

Closing Connection odbc_close(connection) –This function closes the connection obtained and releases the same to the connection pool odbc_close($conn); –The connection defined by $conn is released and no query can be executed using this connection variable hence forth.