|
Consumers and web surfers today require greater levels of interactivity and personalization in the web sites they visit. In order to deliver this kind of technology to web browsers, web designers can use any number of scripting technologies. These scripting languages allow the designer to embed code along with HTML and JavaScript on a web page to provide critical elements that HTML alone cannot such as accessing a database and performing calculations.
A number of scripting languages are currently available. The most popular are: ASP (Active Server Pages) developed by Microsoft and written using Visual Basic Scripting Language; JSP (Java Server Pages) which is part of the J2EE standard and uses Java language scripting; and PHP (originally called Personal Home Page but now called PHP Hypertext Preprocessor) which is designed for use on open-source and Linux web servers. PHP uses its own scripting language which most closely resembles JavaScript, but has much more functionality
The PHP scripting elements are embedded into the HTML using the PHP tag:
?>
Inside this tag, the designer adds to code for PHP to perform. A very simple code would write text to that location in the web page using the print command:
print ¡°PHP can print to the screen¡±;
?>
Like JavaScript or Java, each line in PHP must be terminated with a semicolon. When this code is placed into an HTML page and served to a browser by a Web Server that supports PHP scripting, then the PHP script directive is processed and a page that has all of the dynamic elements is sent to the browser.
One of the strengths of PHP is its tight integration with databases, in particular the open-source MySQL database. For example, creating a connection to a local instance of a MySQL database can be done in PHP with the following:
$dbcnx = mysql_connect("localhost", "root", "mypasswd");
?>
Once the connection is made, a function called mysql_select_db can be used to select the database to be accessed. Finally, mysql_query can be used to execute an SQL query against the database that can retrieve information or update records.
Putting everything together, this PHP page can retrieve all of the information from one database table and display it formatted for the user:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( " Unable to connect to the database server at this time. " ); exit();
}
// Select the products database
if (! @mysql_select_db("prods") ) {
echo( " Unable to locate the product database at this time. " ); exit();
}
?>
Here are all the products in our database:
Unless you are writing the simplest of scripts, they will probably work differently from what you expect of them the first time. This is normal for all programming. What designers need to be able to do is debug and troubleshoot their code to understand where the problems are located and fix them.
The first error that a designer might experience is that the PHP server is simply not working. If you find that not even the simplest PHP scripts do anything, or if a non-PHP error is displayed, you may need to re-examine the installation of the PHP server.
The most common error that programmers face is syntax errors. These occur due to typos or minor errors in the code. Most often syntax errors will be indicated by a PHP error like:
Parse error: parse error in c:\test.php on line 7
This error indicates that somewhere on that line (or perhaps the one before it) there is an incorrect syntax such as a missing semicolon, period, dollar sign, quote, parenthesis, equal sign, etc. If you are unable to confirm the syntax problem, you should consult the official PHP manual at http://www.php.net/docs.php to make sure you have followed all of the guidelines for properly formatting your PHP script. If all of that fails, a number of PHP user forums exist that allow you to post your code for review and have others check it for you.
Another common problem with PHP scripts are logic errors. These occur when the PHP processes to completion, but nothing happens or you don¡¯t get the result you want. In these situations the use of print or echo commands are extremely helpful. You can include these statements at various points to print out key values to make sure you are on the right path before you get to the end result. Once you have the end result correct, you can remove these commands and continue on as expected.
Database connectivity and SQL problems also are very common with PHP because of the common usage of PHP in MySQL web applications. Troubleshooting these problems will also involve use of the echo and print statements to check for errors along the way as in the PHP example above. A common technique is the use of if (!$variable). This checks to see if the variable is not null. If the variable is null, then the previous line which attempted to set that variable to something failed and an error message should be displayed. The exit(); command stops further execution of the PHP script when an error occurs.
The best way to troubleshoot the SQL query is to print out the resulting query and then copy that query to MySQL and run it as a direct query. You can see what should return from MySQL and then modify the query there to get the desired results.
Few tools are currently available for debugging and troubleshooting PHP scripts at the current time. However, as the popularity of PHP web design continue to grow; the amount of developer tools available will likely grow as well. PHP and MySQL are open-source projects, any debugging or developer tools will likely be open-source |
Artical Related:
Piano lesson: chords and cadences
Pets tips: genetic problems in golden retrievers
Photoshop basics: compressing an image
Photoshop basics: managing colors
Photography tips: how a digital camera is different from one with film




