Archive for October, 2007

E-mailThis chapter is (Personal web server) all about using PHP (and,

Wednesday, October 31st, 2007

E-mailThis chapter is all about using PHP (and, in some cases, databases) to send and receive e-mail. It assumes a very basic familiaritywith e-mail systems and protocols such as POP, IMAP, and SMTP. Understanding E-mailE-mail is obviously one of the killer apps of the Internet, and it s beenaround in basically the same form for years. But judging from postingsto the PHP and other mailing lists, there are few more misunderstoodor frustrating topics even now. Because recent developments such ascheaper connectivity, free server operating systems, and the explosionof new domains have made it possible for everyone and his grand- mother to run their own mail servers, there s a lot of frustration togoaround. We don t have the space or the expertise to explain every detail ofe-mail. If you become fascinated with the topic after our necessarilyshort synopsis, you will want to immediately visit the Web site of theInternational Mail Consortium, at www.imc.org, which will explain indizzying detail every abstruse jot and tittle of Internet mail that themost exacting computer scientist s heart could desire. In the mean- time, we ll stick to the quickest and dirtiest of conceptual explanations. To help you visualize the considerable number of moving partsinvolved, we re going to build a simple model mail server. This explanation will mostly use Unix terms, for the simple reasonthat the vocabulary of e-mail was perfected long before Microsoft orLotus came along. Exchange Server is quite likely very similar under- neath it even has a daemon called sendmail but who knows forsure, because we can t lift the hood. Vendors of proprietary softwarealso have the annoying habit of making up their own terminology forthings rather than using the names everyone knows already, a practicethat considerably hinders anyone trying to understand these systemsin an abstract, cross-platform way. Finally, most proprietary mail sys- tems these days are incorporated into very powerful groupwarepackages that confuse the issue even more. For these reasons, Unixmail servers are undoubtedly easier to learn from and implement. We are mentioning specific products so that you can contact the man- ufacturers directly to learn more about these pieces of the puzzle. This does not constitute a recommendation. We have deliberatelychosen to list only products that are available without monetary cost. 3737CHAPTER …In This ChapterUnderstanding e-mailReceiving e-mail with PHPSending e-mail with e-mailapplicationsE-mail gotchas …
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

679Chapter 36PEAR Database Functions .DB_Common::affectedRows(): Returns the number (Florida web design)

Tuesday, October 30th, 2007

679Chapter 36PEAR Database Functions .DB_Common::affectedRows(): Returns the number of rows affected by a query. .DB_Common::disconnect(): Disconnects a database connection. .DB_Common::getAll(): Returns all rows returned by a query. .DB_Common::getAssoc(): Returns all rows as an associative array. .DB_Common::getCol(): Returns all rows in a specified column. .DB_Common::getOne(): Returns the value in the first column of the first row. .DB_Common::getRow(): Returns the first row. .DB_Common::nextId(): Allows you to exercise extra control over the establishment ofunique id values, as in a primary key column. .DB_Common::query(): Sends an SQL query string. Members of the DB_Result classThe members of the DB_Resultclass may be invoked on a result set, which is what existsafter a query is run on a database. .DB_Result::fetchInto(): Extracts a row into a specified variable. .DB_Result::fetchRow(): Extracts the next row. .DB_Result::free(): Destroys the result set. .DB_Result::numCols(): Returns the number of columns in a result set. .DB_Result::numRows(): Returns the number of rows in a result set. SummaryThis chapter showed you how to use PEAR DB, the class with which PHP makes databaseconnectivity more generic. You saw that PEAR DB is designed to make the task of switchingfrom one database server to another extremely easy and that it generally succeeds in thisdesign goal. In many cases, switching connectivity from one database server to anotherrequires the modification of only one word in a whole database-enabled program. The process of connecting to a database via PEAR DB involves establishing a DSN, which isessentially a URL for a database. The DSN specifies the hostname of the database server, aswell as the database name, and the username and password required to gain access to thedatabase. With a valid DSN, it s possible to establish a connection, run a query, and extractrows of values from the query s results before disconnecting. PEAR DB also includesmethods which you should take care to use for detecting error and warning conditions. …
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

678Part IVConnectionsThe program sends the query to the (Web server type)

Tuesday, October 30th, 2007

678Part IVConnectionsThe program sends the query to the database and checks to see if an error message comesback. $returnArray = array(); while ($row = $result->fetchRow()) { $id = $row[0]; $desc = $row[1]; $weight = $row[2]; $packageQty = $row[3]; $unit = $row[4]; $supplierID = $row[5]; $cost= $row[6]; $returnArray[] = array( id => $id, desc => $ desc, weight => $weight, packageQty => $packageQty, unit => $unit, supplierID => $ supplierID, cost => $cost); } $db->disconnect(); return $returnArray; ?> The remainder of the program involves setting up an array called $returnArray. It is filledwith a series of subarrays, making it a two-dimensional array. The subarrays are associativearrays; their keys correspond to column names in the database, and their values come fromeach row of $result. PEAR DB FunctionsThe PEAR DB class is fairly extensive, with members far more numerous than the widely usedones covered already in this chapter s examples. Most of the other members are specialized, and as such come in handy only under particular circumstances. This section summarizes some of the most useful members of the PEAR DB class, but is notcomprehensive. Be sure to refer to http://pear.php.net/manual/en/package.database. phpfor the official list and documentation. Members of the DB classThe DBclass itself is the main PEAR DB class, and is used to represent a connection to adatabase (or an attempt to create one). Methods include: .DB::connect(): Uses a DSN to connect to a database. .DB::isWarning(): Returns true if a connection attempt yielded a warning. .DB::isError(): Returns true if a connection attempt yielded an error. Members of the DB_Common classThe methods of the DB_Commonclass may be invoked on a database connection for such pur- poses as executing queries and getting information from the database. Methods include:
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Free web space - 677Chapter 36PEAR Database FunctionsA complete exampleIt may be

Monday, October 29th, 2007

677Chapter 36PEAR Database FunctionsA complete exampleIt may be beneficial to have a look at a complete function that performs database accessoperations by way of the PEAR DB class. The role of this function, which we ll call getItems.php, is to extract all items from aMicrosoft SQL Server database called INVENTORY_item. To return all items, we need a SELECTstatement that draws all columns out of the INVENTORY _itemtable. Because INVENTORY_itemhas no foreign keys (an assumption made for the purposes of this illustration), extract- ing its data involves only sending a straightforward SELECTstatement to the database servervia a PEAR DB connection. Let s examine getItems.phpline by line to see how this is done. getMessage()); } Using the PEAR DB procedure discussed earlier in this chapter, the program connects to thedatabase server. The program checks for an error condition and aborts if one is found to existas a result of the connection attempt. The program then defines the SQL statement to be run against the database to which a con- nection has been established: $sql = select id, desc, weight, packageQty, unit, supplierID, cost from INVENTORY_item ; That s the SQL query that is to be sent to the database server. Note that we specify thecolumns, even though we want all of them. That way we know what order the columns will be in when results come back. $result = $db->query($sql); if (DB::isError($result)) { $errorMessage = $result- >getMessage(); die($errorMessage); }
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

676Part IVConnectionsQueryBuilding on the successful database (Cool web site) connection, you ll

Sunday, October 28th, 2007

676Part IVConnectionsQueryBuilding on the successful database connection, you ll typically want to run a query againstthe database. That s accomplished with Structured Query Language (SQL), which is describedin detail in Chapter 13. The customary procedure is to write your SQL query as a string and stuff that string into avariable called $sqlor $query: $sql = SELECT cityName FROM cities ORDER BY cityName ; Then use that variable as a parameter for the query()method of your database object (inother words, of the variable that represents the connection to the database), and assign theresults to another variable: $result = $db->query($sql); Once again, allow for the possibility of error: if (DB::isError($result)) $errorMessage = $result->getMessage(); die($errorMessage); } Row retrievalAs a result of the query()operation, $resultcontains a result set. A result set is some number of rows (possibly zero). To extract values from the result set, use a whileloop like this one: $i=0; while ($row = $result->fetchRow()) { $returnArray[$i] = $row[0]; ++$i; } This loop exists to use the fetchRow()function against every row in the result object, thus extracting it. Because we know the result set has only one column (the SQL statementrequested only cityName), we can take the first element of every row array ($row[0] theonly element) and put it into another array, $returnArray. Presumably, we ll do somethinguseful with $returnArraylater, but that s not anything to do with PEAR DB directly. DisconnectionWhen a database connection has served its purpose, use the disconnect()method of thedatabase object to free the resources associated with the connection: $db->disconnect(); It s important to remember to do this; otherwise, the connection remains active until it timesout (a long time). This means that the database server, in an active environment, couldbecome overwhelmed with zombie connections.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

675Chapter 36PEAR Database FunctionsOnce you have the values (Photography web hosting)

Sunday, October 28th, 2007

675Chapter 36PEAR Database FunctionsOnce you have the values available, stringing them together into a properly formatted DSN ispretty simple. The standard format looks like this: $dsn = $phptype://$username:$password@$hostspec/$database ; There are more obscure options available for use in defining DSNs. They re described indetail at http://pear.php.net/manual/en/package.database.db.intro-dsn.php. As always, we recommend storing database connection variables outside the Web tree forgreater security. ConnectionWhen you have a valid DSN, it s a simple matter to tell PEAR DB to establish a connection tothe database the DSN describes. The line of code you need looks like this: $db = DB::connect($dsn); If the connection attempt succeeds, everything s great $dbcontains an object representinga connection to the database, and you can run queries against that object (among other amus- ing and educational activities). Because you re a good programmer, though, you should allowfor errors. Here s how: if (DB::isError($db)) { die($db->getMessage()); } The isError()method returns true if the database object $dbin this case represents aconnectivity error, and false if not (meaning, if the connection succeeded). In this case, thecode aborts if the database connection wasn t successfully established. TipValid Database Identifiers for DSNsHere s a list of all the database management servers supported by PEAR DB, complete with thestrings you should use to identify them in DSNs. .FrontBase (fbsql) .InterBase (ibase) .Informix (ifx) .Mini SQL (msql) .Microsoft SQL Server (mssql) .MySQL (mysql) .Oracle 7/8/8i (oci8) .ODBC (odbc) .PostgreSQL (pgsql) .SyBase (Sybase)
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

674Part IVConnections .Row (Michigan web site) retrieval .DisconnectionThe following subsections explain

Saturday, October 27th, 2007

674Part IVConnections .Row retrieval .DisconnectionThe following subsections explain each in turnData Source Names (DSNs) A Data Source Name (DSN) is simply a text string that describes where a database is and howto access it. A DSN is very much like a URL for a database. Because databases almost alwayshave user-level access control, DSNs specify usernames and passwords. Note that DSNs spec- ify the databaseto which your program is connecting, not the tablewithin the database. In order to form a DSN, you have to come up with a number of values: .The type of database you re connecting to (FrontBase, MySQL, Oracle, or whatever you fancy) .The hostname or IP address of the machine running the database server .The database name .The username you want the software to use .The corresponding passwordVery often, you ll need to access these values in order to create DSNs in several different butrelated PHP programs. For that reason, it often makes sense to assign the literal values tovariables in a special program that s imported elsewhere. Such a program might look like this(call it dbSpecs.php): The only odd bit there is the value for $phptype. Its value is a standard string that corre- sponds to a specific database (MySQL, in this example). The related sidebar shows all validoptions for the database identifier. Having defined your variables in a simple library, when you want to create a DSN, you canimport dbSpecs.phpand have access to its values. The advantage is that if the values indbSpecs.phpever change, you need to modify them in just one place. The import statementis simple: require_once( dbSpecs.php ); With that imported, you can use the values defined in dbSpecs.phpto create a DSN. Remember, though, that you need to register the variables if you create your DSN inside afunction (this is a standard characteristic of PHP variable scoping): global $phptype; global $hostspec; global $database; global $username; global $password;
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

673Chapter 36PEAR Database FunctionsDatabase abstractionWith the application of (Web design)

Friday, October 26th, 2007

673Chapter 36PEAR Database FunctionsDatabase abstractionWith the application of a bit of object-orientation and some general standardization, PEAR DBhas evolved to the point at which changing from MySQL to PostgresSQL (or any of severalother supported databases) involves changing only one line (assuming the SQL queries canremain the same). Here s the same problem solved under PEAR DB: $dsn = mysql://root:sesame@localhost/phpbook ; $db = DB::connect($dsn); if (DB::isError($db)) { die($db->getMessage()); } $sql = SELECT cityName FROM cities ORDER BY cityName ; $result = $db->query($sql); if (DB::isError($result)) { $errorMessage = $result->getMessage(); die($errorMessage); } $i=0; while ($row = $result->fetchRow()) { $returnArray[$i]=$row[0]; $i++; } $db->disconnect(); The point here is that code for the same purpose under PostgresSQL is exactly the same, except for the first line: $dsn = pgsql://root:sesame@localhost/phpbook ; Changing the code for Microsoft SQL Server involves only one modification, as well: $dsn = mssql://root:sesame@localhost/phpbook ; And so on. The next section explains Data Source Names (DSNs) and other important PEARDB concepts in detail. Pear DB ConceptsThere are a number of concepts in PEAR DB that you must understand to work with the classeffectively. These include: .Data Source Names (DSNs) .Connection .Query41
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

672Part IVConnectionsNative database connectivityNative database functions, because they (Web hosting faq)

Friday, October 26th, 2007

672Part IVConnectionsNative database connectivityNative database functions, because they are written in C rather than PHP, will always be thefastest way for a PHP script to connect to a database. However, there is a whole series offunctions for each supported database. In a nutshell, the process involves a series of steps, each customized to the database being used. To connect to a MySQL database, a program uses a command like this: mysql_connect( localhost , root , sesame ); Then, to specify which database it is going to work with, the program issues a command like this: mysql_select_db( phpbook ); Then the program defines and issues a query: $query = SELECT Surname FROM personal_info WHERE ID<10 ; $result = mysql_query($query); With that done, it is a matter of looping through the result set to see what came back from thedatabase: while ($name_row = mysql_fetch_row($result)) { print( $name_row[0] $name_row[1] $name_row[2]
n ); } To do the same work under another database management server, four of the six lines wouldhave to be modified. Here s the same code for PostgresSQL (assuming the same hostnameand other details): pg_connect( localhost , root , sesame ); pg_select_db( phpbook ); $query = SELECT Surname FROM personal_info WHERE ID<10 ; $result = pg_query($query); while ($name_row = pg_fetch_row($result)) { print( $name_row[0] $name_row[1] $name_row[2]
n ); } That can be a real hassle, and the process introduces all kinds of opportunities for error. PEAR DB solves this problem. PEAR DB and PHP5At the time of this writing, changes specific to PHP5 had mostly not yet been incorporated intoPEAR DB. All the major changes in PHP5 except XML affect PEAR DB: the new object model, exception handling, SQLite, and the new mysqli extension. Of these, only SQLite is now beinghandled correctly. Expect changes in PEAR DB when these new features trickle down.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

671Chapter 36PEAR Database FunctionsMySQL and PostgreSQL the (Dedicated web hosting) only adjustments

Thursday, October 25th, 2007

671Chapter 36PEAR Database FunctionsMySQL and PostgreSQL the only adjustments you d have to make would be in the codedirectly concerned with accessing the database opening a connection, sending a query, and so on. You d have to specify that the new database is of the Oracle variety, and providedetails about its hostname and other aspects of its implementation. In reality, to supportnumerous databases, you might have to make some adjustments to your SQL statements aswell, because all database servers differ in the way they implement SQL. Beyond the ANSI- standardized basics, database servers can vary considerably in the syntaxes they consideracceptable. The new database server will have to be configured with the databases, tables, column names, and rows that the migrating application requires. The decision to use a database abstraction layer should always be made on a project-by- project basis. Feel free to heavily discount the opinions of coders who tell you that they are always good or always bad. Also discount those who claim that the benefit of databaseabstraction is that you yourselfwill be able to switch between databases easily. That happensvery rarely (and is usually a sign of much bigger underlying errors in judgment) and shouldtherefore never be a major consideration in designing a system. The actual benefit of sup- porting multiple databases is if your usersneed to use multiple databases. The rule of thumbis, the more users (in the sense of separate installations) of your application there are, themore demand there will be for numerous databases and therefore the more it might gainfrom PEAR DB or one of its competitors. Most standalone Web sites will benefit very little from PEAR DB, because the extra weight ofthe abstraction layer is a much bigger concern than the small possibility of having to changedatabases in the future. Contractors need to weigh the potential gains to them of reusabilityover the potential loss to the client of performance. In situations where you might need tointegrate numerous disparate PHP packages, PEAR DB might help or hurt you. The one case where PEAR DB can clearly be a win is a standalone application that will havemultiple installations, needs to run on as many DBMS as possible, has a developer communitywilling to take on the added work to support multiple databases, and doesn t need to be veryperformative. Many open source and commercial PHP packages such as wikis, blogware, trouble-ticket systems, bulletin boards, and so on do meet all these criteria and wouldtherefore benefit from a database abstraction layer. Portability is almost certainly a much big- ger issue for this type of project than it will be for a Web developer working on a single site although even here, many packages choose their own lighter-weight solutions to databaseabstraction rather than PEAR DB so don t be so dazzled by the fact that some big-namePHP project uses PEAR DB that you forget that your own needs might be very different. One common reason for using PEAR DB is simply because the individual developer happensto prefer a fully object-oriented database connection paradigm. This is fine, but technicalleads and architects should be aware that there are freely available database-specific classesthat may be more performative and maintainable than PEAR DB. The main point stands, however: If you designed your application to use the PEAR DBclasses, very little of your application s PHP code will require modification in order to movethe application from one database management server to another. That s a real advantage if you anticipate numerous users who might wish to use a wide selection of databases to runyour application. Caution41
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.