1 / 27

PHP Programming

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 1995.

vallari
Télécharger la présentation

PHP Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Programming

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

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

  4. 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

  5. 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 */

  6. Sample PHP <html> <body> <?php echo “hello”; ?> </html> The above program will display hello in the browser

  7. 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

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

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

  10. 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;

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

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

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

  14. 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

  15. 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

  16. 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

  17. 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);

  18. 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.

  19. 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

  20. 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.

  21. 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

  22. 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.

  23. 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);

  24. 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

  25. 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.

  26. 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.

  27. 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.

More Related