Jul 22, 2009 Author: SQLmaster

Like Perl, PHP is a scripting language. Unlike Perl, PHP is designed less as a general purpose language than as a language for writing Web applications.The PHP API is used primarily as a means of embedding executable scripts into Web pages.This makes it easy for Web developers to write pages with dynamically generated content.When a client browser sends a request for a PHP page to a Web server, PHP executes any script it finds in the page and replaces it with the script’s output.The result is sent to the browser.This enables the page that actually appears in the browser to change according to the circumstances under which the page is requested. For example, when the following short PHP script is embedded in a Web page, it displays the IP number of the client host that requested the page:

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

As a less trivial and more interesting application, you can use a script to provide up-tothe- minute information to visitors based on the contents of your database.The following example shows a simple script such as might be used at the Historical League Web site. The script issues a query to determine the current League membership count and reports it to the person visiting the site:

<title>U.S. Historical League</title></head><body bgcolor="white">
<p>Welcome to the U.S. Historical League Web Site.


<?php# USHL home pagetry{ $dbh = new PDO("mysql:host=localhost;dbname=sampdb", "sampadm", "secret"); $dbh->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sth = $dbh->query ("SELECT COUNT(*) FROM member"); $count = $sth->fetchColumn (0);print ("

The League currently has $count members.

"); $dbh = NULL; # close connection } catch (PDOException $e) { } # empty handler (catch but ignore errors)? > </body> </html>

PHP scripts typically look like HTML pages with executable code embedded inside tags.A page can contain any number of code fragments.This provides an extremely flexible approach to script development. For example, you can write a PHP script as a normal HTML page initially to set up the general page framework, and then add code later to generate the dynamic parts of the page. PHP actually has multiple types of database interfaces. Among these is a set of lowlevel libraries, each of which works with a single database engine and which make no effort to unify the interface to different engines the way DBI does. Instead, the interface to each engine looks much like the interface for the corresponding C library that implements the low-level API for that engine. For example, the names of the PHP functions that you use to access MySQL from within PHP scripts are very similar to the names of the functions in the MySQL C client library. A more DBI-like approach is available for PHP by using the PHP Data Objects (PDO) extension.This extension provides a more abstract interface to database engines using a two-level architecture similar to that used by DBI. “Writing MySQL Programs Using PHP,” employ the PDO extension for database access


tags: PHP API

views 3900
  1. Add New Comment