3.4 快速retrieve数据
当你不再想用fetchRow()方法来获取数据的时候,Pear DB通过sql语句提供一些特别的方法来返回想要的数据。这些方法有:getOne, getRow, getCol, getAssoc and getAll. 这有一些使用示例:
<?php require_once 'DB.php'; $db = DB::connect('pgsql://postgres@unix+localhost/clients_db'); // ----------------------------------------------------------- // getOne retrieves the first result of the first column // from a query $numrows = $db->getOne('select count(id) from clients'); // ----------------------------------------------------------- // getRow will fetch the first row and return it as an array $sql = 'select name, address, phone from clients where id=1'; if (is_array($row = $db->getRow($sql))) { list($name, $address, $phone) = $row; } // ----------------------------------------------------------- // getCol will return an array with the data of the // selected column. It accepts the column number to retrieve // as the second param. // The next sentence could return for example: // $all_client_names = array('Stig', 'Jon', 'Colin'); $all_client_names = $db->getCol('select name from clients'); // ----------------------------------------------------------- // Other functions are: getAssoc() and getAll(). // For the moment refer to their in-line documentation // at pear/DB/common.php // ----------------------------------------------------------- ?> |
“get*() 系列方法” 可以为你做很多事情, 包括: 发起一个查询, 获取数据和清除结果。请注意所有的Pear DB函数将可能返回一个 Pear DB_error 对象。
3.5 从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)
通过 Pear DB可以从查询结果获得更多有用的数据信息 。这些方法有:
numRows(): 通过一个”SELECT” 查询返回所有数据的数量。
numCols():通过一个”SELECT” 查询返回所有的列。
affectedRows(): 通过(“INSERT”, “UPDATE” or “DELETE”)操作返回所有受影响的数据行数。
tableInfo():通过一个”SELECT” 查询返回一个包含数据信息的数组。
示例:
<?php ... $db = DB::connect($dsn); $sql = 'select * from clients'; $res = $db->query($sql); // Don't forget to check if the returned result from your // action is a Pear Error object. If you get a error message // like 'DB_error: database not capable', means that // your database backend doesn't support this action. // // Number of rows echo $res->numRows(); // Number of cols echo $res->numCols(); // Table Info print_r ($res->tableInfo()); // Affected rows $sql = "delete from clients"; // remember that this statement won't return a result object $db->query($sql); echo 'I have deleted ' . $db->affectedRows() . 'clients'; ?> |
3.6 自动增长(Sequences)
Sequences 为数据行提供独一无二的ID标识。如果熟悉MySQL之类的话,可以把它想象为AUTO_INCREMENT.它非常简单,首先你获取一个ID,然后在这个ID所在的行插入你所需要记录的数据。可以为你的表设置更多的Sequences,只需要保证在任何特殊的表中都使用一样的sequence就行。
<?php ... // Get an ID (if the sequence doesn't exist, it will be created) $id = $db->nextID('mySequence'); // Use the ID in your INSERT query $res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')"); ... ?> |
3.7 Prepare & Execute/ExcuteMultiple
<?php // UNTESTED CODE !!! // // Example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), array(3, 'three', 'tre'), array(4, 'four', 'fire') ); $sth = $dbh->prepare("INSERT INTO numbers VALUES( , , )"); foreach ($alldata as $row) { $dbh->execute($sth, $row); } //Here's an example of a file placeholder: $myfile = "/tmp/image.jpg"; $sth = $dbh->prepare('INSERT INTO images ( , &)'); $dbh->execute($sth, array("this is me", $myfile)); //After I commit a bugfix that I have on my laptop, you can use //parameter arrays in the getXxx methods too: $ver = $dbh->getOne("SELECT stableversion FROM packages WHERE name = ", array($package)); ?> |
3.8 autoCommit, commit and rollback
<?php //examples here ?> |