| DSN連接方式DB目前支持的連接方式如下最完整的格式:phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value別外還有一些縮寫的格式:phptype://username:password@protocol+hostspec:110//usr/db_file.dbphptype://username:password@hostspec/databasephptype://username:password@hostspecphptype://username@hostspecphptype://hostspec/databasephptype://hostspecphptype:///databasephptype:///database?option=value&anotheroption=anothervaluephptype(dbsyntax)phptype其中phptype表示數(shù)據(jù)庫(kù)類型,可用的選項(xiàng)有:dbase  -> dBasefbsql  -> FrontBaseibase  -> InterBaseifx    -> Informixmsql   -> Mini SQLmssql  -> Microsoft SQL Servermysql  -> MySQL (for MySQL <= 4.0)mysqli -> MySQL (for MySQL >= 4.1) (since DB 1.6.3)oci8   -> Oracle 7/8/9odbc   -> ODBC (Open Database Connectivity)pgsql  -> PostgreSQLsqlite -> SQLite | 
| (推薦) | 標(biāo)準(zhǔn)的用來代替數(shù)字(或字符)型變量的參數(shù),它可以自動(dòng)地eacape或根據(jù)當(dāng)前的DBMS系統(tǒng)的需要,來quoted數(shù)據(jù) | 
| ! | stands for a scalar value and will inserted into the statement "as is" | 
| & | 請(qǐng)求一個(gè)已經(jīng)存在的文件,這個(gè)文件的內(nèi)容將用于替換&,常用于保存二進(jìn)制文件或圖象內(nèi)容到數(shù)據(jù)庫(kù)中 | 
2.execute() 把變量傳遞給prepare的SQL語(yǔ)句,然后執(zhí)行它,execute()需要2個(gè)參數(shù),一個(gè)是prepare()調(diào)用時(shí)指定的變量,另一個(gè)就是要傳遞給的變量(可以用數(shù)組)
一個(gè)簡(jiǎn)單的例子
<?php 
// 假設(shè)你已經(jīng)有一個(gè)有效的數(shù)據(jù)庫(kù)連接變量$db …
$sth = $db->prepare(‘INSERT INTO numbers (number) VALUES (?)‘); 
$db->execute($sth, 1); 
$db->execute($sth, 8); 
?>
一個(gè)保存圖象到數(shù)據(jù)庫(kù)的例子
假設(shè)數(shù)據(jù)庫(kù)中有一個(gè)test表,它的結(jié)構(gòu)如下:
CREATE TABLE `test` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  `image` blob,
  `description` text,
  `note` text,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM
insert.php
<?php 
require_once ‘DB.php‘; 
$dsn = ‘mysql://username:password@localhost/test‘; 
$options = array( 
    ‘debug‘       => 2, 
    ‘portability‘ => DB_PORTABILITY_ALL, 
); 
$db =& DB::connect($dsn, $options); 
if (DB::isError($db)) { 
    die($db->getMessage()); 
} 
$db->setFetchMode(DB_FETCHMODE_ASSOC); 
$sth = $db->prepare("INSERT test(name, image) VALUES(?, &)"); 
$db->execute($sth, array(‘jxyuhua‘, ‘D://websamples//PEAR//Database//welcome.jpg‘)); 
if (DB :: isError($db)) { 
    die($db->getMessage()); 
} 
//$res->free(); 
$db->disconnect(); 
?>
存入數(shù)據(jù)庫(kù)之后(注意內(nèi)容大小的限制),你就可以再將內(nèi)容取出來顯示了
顯示的例子
image.php
<?php 
require_once ‘DB.php‘; 
$dsn = ‘mysql://username:password@localhost/test‘; 
$options = array( 
    ‘debug‘       => 2, 
    ‘portability‘ => DB_PORTABILITY_ALL, 
); 
$db =& DB::connect($dsn, $options); 
if (DB::isError($db)) { 
    die($db->getMessage()); 
} 
$db->setFetchMode(DB_FETCHMODE_ASSOC); 
$res = $db->query("select image from test where name = ? order by name", $_REQUEST[‘name‘]); 
if (DB :: isError($res)) { 
    die($res->getMessage()); 
} 
while ($row = $res->fetchRow()) { 
    $data = $row[‘image‘]; 
} 
header("Content-type: image/jpeg"); //如果是其它的圖象格式,如GIF,要做相應(yīng)的更改image/gif
echo ($data); 
$res->free(); 
$db->disconnect(); 
?>
DB允許你一次進(jìn)行一組數(shù)據(jù)庫(kù)操作,例如,你要一次性地插入/更新一批數(shù)據(jù), 
<?php
//假設(shè)數(shù)據(jù)庫(kù)已經(jīng)連接
$alldata = array(array(1, ‘one‘, ‘en‘), 
                 array(2, ‘two‘, ‘to‘), 
                 array(3, ‘three‘, ‘tre‘), 
                 array(4, ‘four‘, ‘fire‘)); 
$sth = $db->prepare(‘INSERT INTO numbers VALUES (?, ?, ?)‘); 
foreach ($alldata as $row) { 
    $db->execute($sth, $row); 
}
?>
以上語(yǔ)句將執(zhí)行等效于下面的SQL語(yǔ)句功能:
INSERT INTO numbers VALUES (‘1‘, ‘one‘, ‘en‘)
INSERT INTO numbers VALUES (‘2‘, ‘two‘, ‘to‘)
INSERT INTO numbers VALUES (‘3‘, ‘three‘, ‘tre‘)
INSERT INTO numbers VALUES (‘4‘, ‘four‘, ‘fire‘)
如果你想更省事一點(diǎn),可以使用executeMultiple()
<?php
//假設(shè)數(shù)據(jù)庫(kù)已經(jīng)連接
$alldata = array(array(1, ‘one‘, ‘en‘), 
                 array(2, ‘two‘, ‘to‘), 
                 array(3, ‘three‘, ‘tre‘), 
                 array(4, ‘four‘, ‘fire‘)); 
$sth = $db->prepare(‘INSERT INTO numbers VALUES (?, ?, ?)‘); 
$db->executeMultiple($sth, $alldata);
?>
有沒有這樣的經(jīng)歷,當(dāng)你在一個(gè)表中加了(或刪除)一些字段之后,你原來的SQL語(yǔ)句就必須要重新寫過,這樣是不是很煩人?
例如,你有一個(gè)user表,它有3個(gè)字段(id, name, country),你以前的INSERT/UPDATE語(yǔ)句:
INSERT INTO user (id, name, country) VALUES (?, ?, ?)
UPDATE user SET id=?, name=?, country=? WHERE ...
當(dāng)你增加了一個(gè)字段(假設(shè)是birthday)之后,你就必須重新你以前的INSERT/UPDATE語(yǔ)句,而這很有可能會(huì)導(dǎo)至BUG的出現(xiàn)(假設(shè)你漏改了某些地方).
現(xiàn)在讓我們來看看怎樣用autoPrepare()/autoExecute()來解決這個(gè)問題
1. autoPrepare()
用autoPrepare(),你不必列出INSERT/UPDATE的詳細(xì)語(yǔ)句,而只要指定它的基本信息
resource autoPrepare (string $table, array $table_fields, integer $mode = DB_AUTOQUERY_INSERT [, string $where = FALSE])
$table               表名
$table_fields        要進(jìn)行操作的字段名列表(數(shù)組類型)
$mode                (DB_AUTOQUERY_INSERT || DB_AUTOQUERY_UPDATE)
$where               WHERE語(yǔ)句,用于過濾數(shù)據(jù)
<?php 
// 假設(shè)你已經(jīng)有一個(gè)有效的數(shù)據(jù)庫(kù)連接變量$db … 
$table_name   = ‘user‘; 
$table_fields = array(‘id‘, ‘name‘, ‘country‘); 
$sth = $db->autoPrepare($table_name, $table_fields, 
                        DB_AUTOQUERY_INSERT); 
if (DB::isError($sth)) { 
    die($sth->getMessage()); 
} 
$table_values = array(1, ‘Fabien‘, ‘France‘); 
$res =& $db->execute($sth, $table_values); 
if (DB::isError($res)) { 
    die($res->getMessage()); 
}
?>
在這個(gè)例子中,autoPrepare()會(huì)把它自動(dòng)翻譯成
INSERT INTO user (id, name, country) VALUES (?, ?, ?)
然后再自動(dòng)調(diào)用prepare()
注意:如果你使用DB_AUTOQUERY_UPDATE, 記得指定$where條件,否則將更新所有的數(shù)據(jù).
2.autoExecute()
autoExecute()是autoPrepare()和execute()的混合體,它可以方便地執(zhí)行INSERT/UPDATE
DB_Result autoExecute (string $table, array $fields_values [, integer $mode = DB_AUTOQUERY_INSERT [, string $where = FALSE]])
$table               表名
$table_fields        要進(jìn)行操作的字段名列表(數(shù)組類型)
$mode                (DB_AUTOQUERY_INSERT || DB_AUTOQUERY_UPDATE)
$where               WHERE語(yǔ)句,用于過濾數(shù)據(jù)
<?php 
// 假設(shè)你已經(jīng)有一個(gè)有效的數(shù)據(jù)庫(kù)連接變量$db … 
$table_name = ‘user‘; 
$fields_values = array( 
    ‘id‘      => 1, 
    ‘name‘    => ‘Fabien‘, 
    ‘country‘ => ‘France‘ 
); 
$res = $db->autoExecute($table_name, $fields_values, 
                        DB_AUTOQUERY_INSERT); 
if (DB::isError($res)) { 
    die($res->getMessage()); 
} 
?>
它執(zhí)行等效于下面的語(yǔ)句
INSERT INTO user (id, name, country)  VALUES (1, ‘Fabien‘, ‘France‘)
是不是很方便簡(jiǎn)直帥呆了!
注意:如果你使用DB_AUTOQUERY_UPDATE, 記得指定$where條件,否則將更新所有的數(shù)據(jù).
|  |