First Steps with PDO (Transfer from: http:netevil.org)

zhaozj2021-02-16  91

22nd May @ 20:51 GMT

First Steps with PDO

Developing PDO and releasing an alpha has sparked a lot of interest already (probably helped along by George ;-)) and we got our first "how does it work" e-mail today. As it happens, I've already written a intro To Pdo for the OTN But Ge George Informs Me That The Can Take a while to publish.

Meanwhile, to avoid being swamped by mail as the word gets out, here are a couple of sample PDO scripts to get you started. Please keep in mind that it is alpha software (although still works pretty well) and requires PHP 5 from CVS ( RC3 Will Work Too, But That Isn't Released Until Next Week.

The API is "grown-up" by default; you get so called "unbuffered" result sets as standard and the prepare / bind / execute API is preferred, although there are some short-cuts already (and some more planned) Note that. You do't need to do any quoting manually sale bound parameters; it is handled for you. You do need to be careful with magic_quotes though (as always).

?. errorCode () and $ stmt-> errorInfo ()) // PDO_ERRMODE_WARNING (php warnings) $ dbh-> setAttribute (PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION); // one-shot query $ dbh-> exec ( "create table test (name varchar (255) Not Null Primary Key, Value Varchar (255)); ");?>

prepare ("INSERT INTO TEST (Name, Value) VALUES (: Name,: Value)"); // Bind PHP Variables to the named Placeholders In The Query // They Are Both Strings That Will Not Be More Than 64 Chars Long $ Stmt-> Bindparam (': Name', $ Name, PDO_PARAM_STR, 64); $ Stmt-> Bindparam (': Value', $ VALUE , PDO_PARAM_STR, 64); // INSERT A Record $ Name = 'foo'; $ value = 'bar'; $ stmt-> execute (); // and another $ name = 'fu'; $ value = 'ba' $ stmt-> execute (); // more if you like, but we're done $ stmt = null;?> prepare ('SELECT NAME, Value from test where name =: what'); $ stmt-> bindparam ('what', $ what); $ stmt-> execute (); // get the row using PDO_FETCH_BOTH (default if not specified as parameter) // other modes: PDO_FETCH_NUM, PDO_FETCH_ASSOC, PDO_FETCH_OBJ, PDO_FETCH_LAZY, PDO_FETCH_BOUND $ row = $ stmt-> fetch (); print_r ($ row); $ stmt = null;? >

prepare ('select name, value from test'); $ stmt-> execute (); while ($ row = $ stmt-> fetch (PDO_FETCH_ASSOC )) {Print_R ($ row); $ stmt = null;?>

Prepare ('Select Name, Value from Test'); $ Stmt-> Execute (); $ Stmt-> BindColumn ('Name' $ name); $ stmt-> bindcolumn ('value', $ value) 'While ($ stmt-> fetch) {echo "name = $ name, value = $ value / n";}?>

OH, how do you get and install it?

Grab a php 5 snapshot from http://snaps.php.net (or head from cvs).

./configure --prefix = / usr / local / php5 --with-zlib .... make make install export path = "/ usr / local / php5 / bin: $ PATH" / usr / local / php5 / bin / Pear install -f pdo [now add extension = PDO.SO to php.ini] / usr / local / php5 / bin / pear install -f pdo_mysql [now add extension = PDO_MYSQL.SO to php.ini] / usr / local / PHP5 / BIN / PHP -M

There Are Other Drivers; Search Pecl for More. If You're Running Windows, Just Grab The Win32 Snap and The PDO DLLS from PECL BINARIES for PHP 5.

Credits: Thanks to Marcus, George, Ilia and Edin.

Please try to avoid asseking too; documentation.

转载请注明原文地址:https://www.9cbs.com/read-15847.html

New Post(0)