The query() method on a database object sends SQL to the database: $result = $db->query(sql); A SQL statement that doesn’t query the database (e.g.,INSERT,UPDATE,DELETE) returns theDB_OKconstant to indicate success. SQL that performs a query (e.g.,SELECT) returns an object that you can use to access the results. You can check for success withDB::isError(): $q = $db->query($sql); Fetching Results from a Query PEAR DB provides two methods for fetching data from a query result object. One returns an array corresponding to the next row, and the other stores the row array into a variable passed as a parameter. Returning the row The fetchRow() method on a query result returns an array of the next row of results: $row = $result->fetchRow([ mode ]); This returns either an array of data, NULLif there is no more data (or none to begin with—an empty result), orDB_ERRORif an error occurred. Themode parameter controls the format of the array returned, which is discussed later. This common idiom uses thefetchRow()method to process a result, one row at a time, as follows: while ($row = $result->fetchRow()){ Storing the row The fetchInto() method not only gets the next row, but also stores it into the array variable passed as a parameter: $success = $result->fetchInto(array, [mode]); LikefetchRow(),fetchInto()returnsNULLif there is no more data, orDB_ERRORif an error occurs. The idiom to process all results looks like this withfetchInto(): while ($success = $result->fetchInto($row)) {
blog comments powered by Disqus |
|
|
|
|
|
|
|