Archive for January, 2008

Chapter 15 . (Web and email hosting) Perl Development 427 $query =

Thursday, January 31st, 2008

Chapter 15 . Perl Development 427 $query = SELECT host,user FROM user WHERE user = $quoteduser ; $sth = $dbh->prepare($query); $sth->execute(); while (($host,$username) = ( $sth->fetchrow_array() ) ) { if ($host eq ) { print User $user has a blank host entry.n ; } else { print User $user is allowed to connect from: $hostn ; } } } sub priv { # This subroutine looks up the grants for the given # user/host pair. ($user,$host) = @_; $query = SHOW GRANTS FOR $user@$host ; $sth = $dbh->prepare($query); $sth->execute(); while ($grant = ( $sth->fetchrow() ) ) { print $grantn ; } } Figure 15-42: Usage examples of the MySQL User Management program I ve built with the DBI
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

426 Part IV . Development To make the (Adelphia web hosting)

Thursday, January 31st, 2008

426 Part IV . Development To make the program even more useful I ve added the ability to look up the privileges that a given user/host pair has. This shows an example of another type of statement being performed on the MySQL database other than a SELECT. The program is too large to fit into a figure illustration but I ve included the program here and also some sample output in Figure 15-42. #!/usr/bin/perl use DBI; # This program looks up the hosts from which a # given user is allowed to connect. # Also looks up grants for a user, given the @ symbol # in the argument list. $user = $ARGV[0]; # Test to make sure the user entered a username. if (! $user) { print No user specified. Usage:
n ; } $dbh = DBI->connect( DBI:mysql:mysql: ,undef, evh5150 ); if (! $dbh) { print Connection failed!!nn ; die(); } # If there s an @ symbol then the user wants to look up # grants. if ($user =~ m/@/) { ($user,$inhost) = split(/@/, $user); # Make sure the username and host are safe to # send to MySQL. $quoteduser = $dbh->quote($user); $quotedhost = $dbh->quote($inhost); &priv($user,$inhost); } else { # User wants to simply lookup the hosts allowed # for given username. # Make sure the username is safe to # send to MySQL. $quoteduser = $dbh->quote($user);
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Chapter 15 . (Web hosting providers) Perl Development 425 Figure 15-40:

Wednesday, January 30th, 2008

Chapter 15 . Perl Development 425 Figure 15-40: A small program to query the hosts for a given user in the MySQL grants database Figure 15-41: Adding error checking and other improvements to a small user management program with the Perl DBI
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

424 Part IV . (Personal web server) Development Figure 15-39: The

Tuesday, January 29th, 2008

424 Part IV . Development Figure 15-39: The quote() method serves to make strings SQL safe. Building Basic Applications I ve been making basic applications throughout this chapter inside examples of error messages, queries, and updates. This section expands on some of those examples to show a basic Perl-based DBI application that uses functions and methods discussed in the previous section. The goal is to prepare you for building the applications needed to manage an e-commerce Web site in a later section. MySQL User Manager An earlier example created a small application to look at the hosts from which a given user is allowed to connect. I ll expand on that example here, making a more powerful user manager to examine the privileges that a given user/host combination has. As a starting point, Figure 15-40 illustrates the earlier program. This program, though fine for the examples, is woefully inadequate when it comes to error checking. For example, if the host field is blank, the program evaluates the while loop as false and stops iterating through it. Further, no input validation is done. I ve made changes to the program to make it check for errors and continue through the while loop even if the host is blank. I m doing some shenanigans on the actual query to make sure that it always returns something regardless of whether the hostis blank. I added the user to the query results so as to make it always evaluate true for the while loop. The program is shown in Figure 15-41.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Chapter 15 . Perl Development 423 Figure 15-38: (Top web site)

Monday, January 28th, 2008

Chapter 15 . Perl Development 423 Figure 15-38: Using a file handle to dump the results to a file instead of to STDOUT Though not technically necessary for the MySQL DBD, the finish()method can be used to recycle statement handles and improve the readability of your code. The finish() method releases the remaining results from a statement handle and frees it for another use. The MySQL DBD does not require you to finish before issuing another prepare statement, but doing so can greatly assist in tracing code if you need to go back and examine the code at a later date. Finally, if you have to clean up input received from a user or through a Web form, you should know about the quote()method that makes strings safe for use inside an SQL statement. It takes a sole argument the string to work with and results in an SQL-safe string. Figure 15-39 shows an example of the quote() method. Note When using the quote() method, you do not need to include quotes around the strings inside queries. Notice, for example, the change in the way the variable $safeuser is called in Figure 15-39.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

422 Part IV . Development Figure 15-36: Setting (Web server address)

Sunday, January 27th, 2008

422 Part IV . Development Figure 15-36: Setting the maximum length to 8 with the second argument of the dump_results() method Figure 15-37: Using an odd combination of characters as a field separator and manually setting the line break to a newline
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Chapter 15 . Perl Development 421 The first

Saturday, January 26th, 2008

Chapter 15 . Perl Development 421 The first argument, $sth or the statement handle, is required. The second argument, represented by $length in the example, is the maximum length for the results to print. The third and fourth arguments specify how you want to separate the lines and fields respectively. The default for a line break is the newline (n), and the default for a field separator is a comma. The final argument, represented by $filedes, is a pointer to a file handle to indicate a file to which the results should be dumped (instead of to the screen) on STDOUT. You don t save the results of the dump_results() method; rather, you use Perl s print function to print them. More than one figure is necessary to fully illustrate the syntax and use of the dump_results()method. Figure 15-35 shows a basic instance; Figure 15-36 shows a basic usage. Figure 15-36 demonstrates setting a maximum length for the second argument. Figure 15-35: A basic use of the dump_results() method Figure 15-37 shows a usage of field separators and the manual setting of line breaks; Figure 15-38 demonstrates the use of a file handle to dump results into a file instead of to the screen.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

420 Part IV (Vps web hosting) . Development Figure 15-33 illustrates

Friday, January 25th, 2008

420 Part IV . Development Figure 15-33 illustrates the fetchrow_arrayref() method. Notice that the first call retrieves the host from the query, and the second call actually retrieves the password corresponding to the second entry or second row of results. The third method call retrieves an entire row of results from the query at once. Not all interactions with the database require both a prepare()and execute() method call. The do()method enables you to execute statements without having to make two separate calls as with prepare() and execute(). Like the prepare() and execute() methods, do() can accept placeholders. The placeholders are question marks and should be included as the last argument of the do()method call. Figure 15-34 shows an example of do(). You cannot retrieve results using a do() method call, so it is not appropriate for SELECT statements. The do() method does return the number of rows affected by the statement. Therefore, it can be helpful to store this value for sanity checking. In other words, you can compare the number of returned rows to ensure that the value makes sense for the given query. Note Figure 15-34: Using the do() method call to execute a statement without having to prepare it in a separate statement Another method, dump_results(), can assist to print results for testing purposes while you are programming. dump_results() calls another method, neat_string(), to format the results and make them look nicer as output. The dump_results() method can take up to five arguments. The syntax is as follows: $result = DBI::dump_results($sth, $length, $line_break, $field_break, $filedes);
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.

Multiple domain web hosting - Chapter 15 . Perl Development 419 Figure 15-32:

Thursday, January 24th, 2008

Chapter 15 . Perl Development 419 Figure 15-32: The fetchrow_array() method is frequently used as a means to retrieve results from a query. Figure 15-33: An example of the fetchrow_arrayref method for retrieving results from a query
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

418 Part IV . Development Figure (Crystaltech web hosting) 15-30 is

Wednesday, January 23rd, 2008

418 Part IV . Development Figure 15-30 is a small useful program for determining the hosts that a user is allowed to connect from. The program takes the username to search for as an argument and returns the names of the hosts for which the user has an entry table of the MySQL grants database. If the host field of the user table is blank, the program from Figure 15-30 may return incomplete or incorrect results. Figure 15-31 shows an example of the fetchall_arrayref()method for retrieving query results. The method gets all the results at once and places them in an array with each row contained as part of the array. As this is a multi-dimensional array, the results need to be accessed as such. in the user Note Figure 15-31: The fetchall_arrayref() method in action Figure 15-32 modifies the same program from the previous two figure illustrations to use the fetchrow_array() method. This method is frequently used for retrieving results where the SELECT statement queries for more than one column. The final two methods, fetchrow_arrayref() and fetchrow_hashref(), both retrieve results as references to arrays or hashes, respectively. With fetchrow_hashref() the first row of results contain the field names for the values with the actual values being stored thereafter. An example of the fetchrow_arrayref()method is shown in Figure 15-33.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.