谢谢,雪悟,二球.
Pear DB 新手入门指南3
上一篇 /
下一篇 2006-12-18 17:53:32
/ 个人分类:全是帅的不能再输啦
3.4 快速retrieve数据
-D7`&?~fSiz&c0PHPChina 开源社区门户+K7P?YDWN当你不再想用fetchRow()方法来获取数据的时候,Pear DB通过sql语句提供一些特别的方法来返回想要的数据。这些方法有:getOne,getRow,getCol,getAssocandgetAll.这有一些使用示例:PHPChina 开源社区门户B3S2efr
{+eai
<?php !h
Q(k$a@4f!_0require_once 'DB.php'; w(iaS!RD)lOg%PI"W0$db = DB::connect('pgsql://postgres@unix+localhost/clients_db'); zZ
Jc
T8R+l_0// ----------------------------------------------------------- *^z(ZK7|bv(TA-R5R0// getOne retrieves the first result of the first column 5k
Dt.ob ^w0// from a queryPHPChina 开源社区门户q@U0KBS2@
$numrows = $db->getOne('select count(id) from clients');PHPChina 开源社区门户 bV2s9g(a/LN
// -----------------------------------------------------------PHPChina 开源社区门户/]E&F&f}4YIuo
// getRow will fetch the first row and return it as an arrayPHPChina 开源社区门户7m dN'SX M'x
$sql = 'select name, address, phone from clients where id=1'; _:{9O)G
^_0if (is_array($row = $db->getRow($sql))) { ~{1Km
?o&Ga0 list($name, $address, $phone) = $row; *O,lN^vg8x1U0} u!E2XZI@s0// -----------------------------------------------------------PHPChina 开源社区门户 C$D.?7]w9mG
// getCol will return an array with the data of the ko"`-?;Ff[0// selected column. It accepts the column number to retrievePHPChina 开源社区门户'xL@Q(F{)Y
// as the second param.PHPChina 开源社区门户?s)VcX x)tb
// The next sentence could return for example: %|D6gx me4F^0// $all_client_names = array('Stig', 'Jon', 'Colin');PHPChina 开源社区门户rQ8~4f6Cl
$all_client_names = $db->getCol('select name from clients');PHPChina 开源社区门户o+_%l#t.hm9f
// ----------------------------------------------------------- _IXA-m3G1P:P P0// Other functions are: getAssoc() and getAll(). -f P*}-IE%`H([#RW[0// For the moment refer to their in-line documentation O'GtocK0// at pear/DB/common.php _U8D8gZy`O.jP0// ----------------------------------------------------------- o#VN8]8DG(Om9C0?>
|
PHPChina 开源社区门户0^7r+?I*NP"get*()系列方法"可以为你做很多事情,包括:发起一个查询,获取数据和清除结果。请注意所有的Pear DB函数将可能返回一个Pear DB_error对象。PHPChina 开源社区门户&X'}5v
wD4Zl0fKDFbx
3.5 从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)PHPChina 开源社区门户I4M?2{&S
PHPChina 开源社区门户Q@M:V
E)a7]d通过Pear DB可以从查询结果获得更多有用的数据信息。这些方法有:PHPChina 开源社区门户)YS6w9`(Z;`
- numRows():通过一个"SELECT"查询返回所有数据的数量。
- numCols():通过一个"SELECT"查询返回所有的列。
- affectedRows():通过("INSERT", "UPDATE" or "DELETE")操作返回所有受影响的数据行数。
- tableInfo():通过一个"SELECT"查询返回一个包含数据信息的数组。
PHPChina 开源社区门户os.Y#B.uQ示例:
Zq}NF'qrp/K.w*g8w0
<?phpPHPChina 开源社区门户3A*oL:r"EY
...PHPChina 开源社区门户Y!? @jg
$db = DB::connect($dsn); WYNG,A{~3[K"j0$sql = 'select * from clients'; #b{b*p(?.fk/{0$res = $db->query($sql);PHPChina 开源社区门户v@Bm4h[*D/AS
// Don't forget to check if the returned result from yourPHPChina 开源社区门户4\w|0B6I
// action is a Pear Error object. If you get a error messagePHPChina 开源社区门户{7Ic:P)T:lU1u
// like 'DB_error: database not capable', means that -[edVw%i'q@0// your database backend doesn't support this action. #M5huIhU*}ck0// M2j-M!]m1B0// Number of rows '`;W|,c H9x+}!R8n3j0echo $res->numRows(); &E0X/j%k}9R0// Number of colsPHPChina 开源社区门户'o'?x&eq@N
~-Z
echo $res->numCols();PHPChina 开源社区门户{Iy,`9^kY/hd
// Table InfoPHPChina 开源社区门户K
~[Vk:C!{#Fj
print_r ($res->tableInfo());PHPChina 开源社区门户pA+\&\!v#Ra.@:N
// Affected rowsPHPChina 开源社区门户
s2f.?/uB9EYw#l
$sql = "delete from clients"; t#}5BY.jR,a#]Y0// remember that this statement won't return a result object !g%XdC7`0$db->query($sql); [] L3Ks0echo 'I have deleted ' . $db->affectedRows() . 'clients';PHPChina 开源社区门户Lt9t"r:C
?>
|
PHPChina 开源社区门户]"J'C5m{
3.6 自动增长(Sequences)
w pZ*Z9@b2_d0PHPChina 开源社区门户`W vx:FI R*Q*@O
\Sequences为数据行提供独一无二的ID标识。如果熟悉MySQL之类的话,可以把它想象为AUTO_INCREMENT.它非常简单,首先你获取一个ID,然后在这个ID所在的行插入你所需要记录的数据。可以为你的表设置更多的Sequences,只需要保证在任何特殊的表中都使用一样的sequence就行。
-tT"SZT+Fu$XT0
<?php k8~9g/p(MQ.RR0... 7nwH/rQz-MH
^O0// Get an ID (if the sequence doesn't exist, it will be created) gtGV+\o0$id = $db->nextID('mySequence');
N3f
B3bgv0PHPChina 开源社区门户PTm@.sJ
_&C
// Use the ID in your INSERT query #f-B5n7`t6XK7Z0$res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')"); 6U.]6j0HukQ8{0...PHPChina 开源社区门户h$g0L+X:}t'Fv o
?>
|
ea,IATPQ._03.7 Prepare & Execute/ExcuteMultiplePHPChina 开源社区门户/Y)A*yi~Md
<?php +N%[$Fg5\
xe9?L0// UNTESTED CODE !!!PHPChina 开源社区门户n-U$C8Gz'R
// 6@&w!Ryq`*M
D0// Example inserting data /K1g:xkl6^0$alldata = array(PHPChina 开源社区门户4B^mv/P
E!b
array(1, 'one', 'en'),PHPChina 开源社区门户#f,^5T/[.rd xrz
array(2, 'two', 'to'),PHPChina 开源社区门户
m;z'XX?
array(3, 'three', 'tre'),PHPChina 开源社区门户6v&l}5h/q,u
array(4, 'four', 'fire')PHPChina 开源社区门户AV8`)s"^Q_5G
);PHPChina 开源社区门户PT'j?!d]9E8O_
$sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)");PHPChina 开源社区门户@o ~[C0}
foreach ($alldata as $row) {PHPChina 开源社区门户w(Nri T;W&o
$dbh->execute($sth, $row);PHPChina 开源社区门户7F9@T
AQ
Yxu.J
}PHPChina 开源社区门户nJ,h%i&pDL/I)X
//Here's an example of a file placeholder:PHPChina 开源社区门户s"@"QL%O8bR8Xj
g
$myfile = "/tmp/image.jpg";PHPChina 开源社区门户]'i L
hAa!ZOS
$sth = $dbh->prepare('INSERT INTO images (?, &)'); $iR6H0x&K*X0$dbh->execute($sth, array("this is me", $myfile)); 2Rct8pj8^z(ud0//After I commit a bugfix that I have on my laptop, you can use
|