PEAR DB provides a number of methods that perform a query and fetch the results in one step: getOne(), getRow(), getCol(),getAssoc(), andgetAll(). All of these methods permit placeholders. ThegetOne()method fetches the first column of the first row of data returned by an SQL query: $value = $db->getOne(SQL [, values ]); For example: $when = $db->getOne("SELECT avg(pub_year) FROM books"); echo "The average book in the library was published in $when"; The average book in the library was published in 1974 ThegetRow()method returns the first row of data returned by an SQL query: $row = $db->getRow(SQL [, values ]]); This is useful if you know only one row will be returned. For example: list($title, $author) = $db->getRow( The getCol() method returns a single column from the data returned by an SQL query: $col = $db->getCol(SQL [, column [, values ]]); Thecolumn parameter can be either a number (0, the default, is the first column), or the column name. For example, this fetches the names of all the books in the database, ordered by the year they were released: $titles = $db->getAll("SELECT title FROM books ORDER BY pub_year ASC"); The getAll() method returns an array of all the rows returned by the query: $all = $db->getAll(SQL [, values [, fetchmode ]]); For example, the following code builds a select box containing the names of the movies. The ID of the selected movie is submitted as the parameter value. $results = $db->getAll("SELECT bookid,title FROM books ORDER BY pub_year ASC"); All theget*()methods returnDB_ERRORwhen an error occurs.
blog comments powered by Disqus |
|
|
|
|
|
|
|