Four PEAR DB methods provide you with information on a query result object: numRows(), numCols(), affectedRows(), andtableInfo(). ThenumRows()andnumCols()methods tell you the number of rows and columns returned from aSELECT query: $howmany = $response->numRows(); TheaffectedRows()method tells you the number of rows affected by anINSERT,DELETE, orUPDATE operation: $howmany = $response->affectedRows(); ThetableInfo()method returns detailed information on the type and flags of fields returned from aSELECToperation: $info = $response->tableInfo(); The following code dumps the table information into an HTML table: // connect $sql = "SELECT * FROM BOOKS"; $q = $db->query($sql); $info = $q->tableInfo(); function a_to_table ($a) { Figure 8-2 shows the output of the table information dumper.
Sequences Not every RDBMS has the ability to assign unique row IDs, and those that do have wildly differing ways of returning that information. PEAR DB sequences are an alternative to database-specific ID assignment (for instance, MySQL’s AUTO_INCREMENT). The nextID() method returns the next ID for the given sequence: $id = $db->nextID(sequence); Normally you’ll have one sequence per table for which you want unique IDs. This example inserts values into thebookstable, giving a unique identifier to each row: $books = array(array('Foundation', 1951), foreach ($books as $book) { A sequence is really a table in the database that keeps track of the last-assigned ID. You can explicitly create and destroy sequences with thecreateSequence()anddropSequence()methods: $res = $db->createSequence(sequence); The result will be the result object from the create or drop query orDB_ERRORif an error occurred.
blog comments powered by Disqus |
|
|
|
|
|
|
|