Archive for January, 2008

Chapter 15 . Perl Development 417 (Abyss web server) Another function

Tuesday, January 22nd, 2008

Chapter 15 . Perl Development 417 Another function in Figures 15-27 through 15-29 is of particular interest: fetchrow(). This and several related functions retrieve results from executed MySQL statements by using the DBI. The fetchfunctions are listed in Table 15-1. Table 15-1 Fetch functions to retrieve statement results Function name Description fetchrow() Retrieves one value. Useful where one value is selected as a result. fetchrow_array() Retrieves results and places them in an array. fetchrow_arrayref() Retrieves data as a referenced array. fetchall_arrayref Retrieves all data as a reference to an array. fetchrow_hashref() Retrieves data as a hash. Operates similarly to fetchrow_arrayref. I believe that examples of the different fetch functions serve better than a written explanation. For the examples in Figures 15-27 through 15-29, I retrieved only the first row of results. If you want to retrieve more than one row using the fetchrow() method, you have to loop through the results with a function such as while, as shown in Figure 15-30. Figure 15-30: Retrieving all the results for the query, using a while loop and fetchrow
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

416 Part IV . Development (Web hosting isp) prepare(). Alternatively, the

Monday, January 21st, 2008

416 Part IV . Development prepare(). Alternatively, the substitution can take place at execution time by supplying an argument to the execute() call. Figure 15-28 shows an example of this behavior. Figure 15-28: Using a placeholder to substitute a value at execution time for a SELECT statement An example of the bind_paramfunction is shown in Figure 15-29. Figure 15-29: Using the bind_param function to substitute values in a prepare() call Naturally, you could substitute variables for the values in the bind_param argument list. The bind_param argument list also takes an optional third argument for the type of data (such as CHAR, NUMERIC, and so on).
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web hosting solutions - Chapter 15 . Perl Development 415 Functions with

Sunday, January 20th, 2008

Chapter 15 . Perl Development 415 Functions with the DBI So far, I ve discussed four functions as an introduction to the DBI: connect() disconnect() err() errstr() I ve used some other functions in examples. This section examines those functions, along with others for use with the DBI and MySQL DBD. Going back to examples shown in previous figures in this chapter you ll see the creation of a statement handle, $sth, and the use of the prepare() method along with the execute()method. The prepare()method takes an SQL statement and stores or parses it for later execution by the DBI. The execute() method then takes that prepared statement and runs it against the database engine. The execute() method returns the number of rows affected for non-SELECTstatements. With a SELECT statement a Truevalue is returned upon successful execution. The examples of the prepare()and execute() methods shown previously were done in such a manner as to produce an error. Therefore, I ll leave those examples alone and show a new example program with a successful prepare()and execute() method call, as shown in Figure 15-27. Figure 15-27: A successful execution of a MySQL statement using the Perl DBI The prepare() and execute()methods both take optional arguments or placeholders that are substituted into the actual statement at execution time, much like a variable would be. The placeholder is the question mark (?). The bind_param() method substitutes the actual values for the question-mark placeholder when using
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Web hosting account - 414 Part IV . Development Figure 15-25: Using

Saturday, January 19th, 2008

414 Part IV . Development Figure 15-25: Using manual error checking and the handle-level error methods Using handle-level error methods you could assign the errresults to variables for later testing. An example of a program using this type of test is shown in Figure 15-26. Figure 15-26: Using handle-level error methods and assigning one to a variable for later use
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web space - Chapter 15 . Perl Development 413 If you

Friday, January 18th, 2008

Chapter 15 . Perl Development 413 If you choose to disable the error-checking feature, you must perform error checking and handling manually. This can be achieved in numerous ways including the die or warn functions. Another method for manual error checking is the use of an IF test. Because most operations with the DBI return a value of undef for a failure, if the value from the operation does not exist, then you can assume that the execution failed. If it evaluates true, then you can be assured that the database handle was created. Figure 15-24 shows some examples of manual error checking. Tip I ve been using an IFtest in the examples throughout this chapter to test whether the $dbh database-handle variable evaluated to true or false. Figure 15-24: Using manual error-checking methods Built-in error status methods There are built-in methods for working with error status messages. The example program in Figure 15-24 showed one of these, errstr. Another method that I ll highlight is the err()method. The errstr()method contains the string text message of the error. The err()method contains the error number corresponding to the error. The errstr() and err()methods exist both at the DBI level and at the handle level. At the DBI level, as used in Figure 15-24, the methods contain the information for the last-used handle. The handle-level methods are called as shown: $dbh->errstr(); $dbh->err(); Modifying the program from Figure 15-24 to use the handle methods is quite simple, as shown in Figure 15-25.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

412 Part IV . Development Figure 15-22: Setting (Space web hosting)

Thursday, January 17th, 2008

412 Part IV . Development Figure 15-22: Setting PrintError to 0 in the %attr attribute hash to prevent duplicate printing of the error message You can also toggle the behavior of the PrintErrorand RaiseError attributes within the program. For example, if you want to disable all error checking for a certain statement you can set the values of PrintError and RaiseErrorto 0 and then re-enable one or both after the statement executes. Figure 15-23 shows an example that disables all error checking by the DBI, performs a statement, and then re-enables the default PrintErrorerror-checking method. Figure 15-23: Selective error checking within the same program
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Web server extensions - Chapter 15 . Perl Development 411 Though disabled

Thursday, January 17th, 2008

Chapter 15 . Perl Development 411 Though disabled by default, the RaiseErrorattribute causes the program to terminate immediately upon encountering an error with the DBI. By creating an attribute hash, %attr, and setting RaiseError to 1, the RaiseErrorattribute becomes operational and thus terminates the program if an error is encountered. Figure 15-21 shows an example of the same program from Figure 15-20 with the %attr hash created and then appended to the database handle initialization. Tip Note the use of the backslash to escape the % in the database handle creation. Figure 15-21: Using RaiseError to terminate a program immediately if an error occurs Notice in Figure 15-21 that the same error message is printed twice. This is expected behavior. Remember that PrintErroris enabled by default. Therefore, when an error occurs, PrintError sees it and prints the error. Only when RaiseError handles the error does the program terminate. If you want to disable PrintError, set its value to 0, as shown in Figure 15-22.
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

410 Part IV . Development Handling errors with

Wednesday, January 16th, 2008

410 Part IV . Development Handling errors with the DBI In most instances, error handling is automatic with the DBI. This was evidenced by earlier Figures and examples in this chapter. The error messages look quite similar to those you would see with other MySQL tools such as the CLI or mysqladmin. Sometimes you may want to disable this automatic error-checking in favor of manual error checking (or a combination of automatic and manual). Error handling is configured using the attributes or elements discussed in a previous section. There are two attributes of DBI error-checking: PrintError and RaiseError. By default, PrintError is enabled. PrintError simply prints the error message to the screen and continues execution of the program as if nothing had happened. This is bad if your program is relying on information from the database and a statement or command fails to execute. To illustrate this behavior, take a look at the program in Figure 15-20. In this program, I query a database for a number and then perform a computation based on that number. I allow PrintError to perform the error checking in the program, but do no further error handling myself. You can see the results of the program in action in Figure 15-20 as well. The program happily continues, even though the query execution failed. If these results were to be automatically e-mailed to me using a scheduling program such as cron, I wouldn t see these error messages. Figure 15-20: A program continues execution in the event of an error when using the default PrintError for error-checking.
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

Chapter 15 . Perl Development 409 I d like (Web hosting servers)

Tuesday, January 15th, 2008

Chapter 15 . Perl Development 409 I d like to introduce the final portion of database handle creation with the DBI and MySQL DBD. The final portion is optional and consists of attributes or elements that affect the behavior of the handle itself. Therefore, adding this onto the $dbh definition I ve been using: $dbh = DBI->connect($datasource, $credentials, %elements); A number of elements are available here, most notably those that define how errors are handled and what to do with extra blank spaces for CHARcolumn types. I ll examine error handling in the next section; therefore I ll save further discussion of the optional elements until then. Up until now, I ve looked at methods for connecting to the database. By now, you have more than enough information to connect to a database. However, disconnecting from a database is important as well. Normally, when your program exits, the disconnection is automatic thus saving the programmer the work of having to explicitly issue a disconnect command. In practice, however, always explicitly disconnect your database handles. This is because there are times when using multiple handles in a program that you may need to disconnect old handles to preserve resources. If you do not issue an explicit disconnect, Perl disconnects the handle as part of the DESTROY process when a program terminates. You may see an error or warning like: Database handle destroyed without explicit disconnect This should remind you to go back and issue a disconnectstatement within your program. The syntax for the disconnect method is: $dbh->disconnect; Therefore, you can see that it is possible to disconnect only certain database handles. For example, if your program has three database handles, $dbh0, $dbh1, $dbh2, you could simply issue a disconnect() to the specific handle or handles that you no longer need. You may want to check the return status of the disconnection to make sure that it was successful. You can do this by checking the return value from the disconnect call, using the following command: $dis = $dbh->disconnect; If the value of $dis evaluates true, then the disconnection was successful; otherwise the disconnection failed.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

408 Part IV (My space web page) . Development Of course, you

Monday, January 14th, 2008

408 Part IV . Development Of course, you could supply the credentials inside one variable as well (see Figure 15-18). Figure 15-18: Creating the proper credentials inside a variable. $credentials is also a valid way to connect. Also as the credentials portion of the connection can be supplied in a variable, the Datasource could also be supplied in whole or in part by variable interpolation. For example, you could define the host as $host, the database name as $database, and so forth: You could even supply the entire Datasource as a variable, $datasource (or the name of your choosing.) Figure 15-19 illustrates some of these points. Figure 15-19: Specifying portions of the Datasource through the use of variables can help to make the program more readable. Examining what I have defined so far: $dbh = DBI->connect($datasource, $credentials);
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.