676Part IVConnectionsQueryBuilding on the successful database (Cool web site) connection, you ll
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.