[不是轉(zhuǎn)貼,是收藏][喜悅原創(chuàng)首發(fā)]php6展望 && PDO的使用
[喜悅原創(chuàng)首發(fā)]php6展望 && PDO的使用
先展望php6(當(dāng)然也可能是php5.2之類,暫未確定)
去掉的東西(感覺有必要提的)
2.1 register_globals
這個影響安全,又不好處理.
2.2 magic_quotes
這個本意很好,但反對的聲音很多...
2.3 safe_mode
這個東西被E_CORE_ERROR代替了.
2.11 register_long_arrays, HTTP_*_VARS
這個東西影響速度
特別的
1. Unicode
這個東西好!不用再擔(dān)心很多了.
3.1 XMLReader / XMLWriter in the distribution, on by default
以后處理rss之類的就更容易了
3.2 Move non-PDO DB extensions to PECL
下面特別討論.
3.3 Move ereg to PECL
實際preg,ereg差不多.用兩個等于浪費.
其余的參見(http://www.php.net/~derick/meeting-notes.html)
============================================
下面描述一下,在php6,沒有mysql_*,我們怎么過.
首先要連接mysql數(shù)據(jù)庫
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
//如果你想連mssql:
//mssql:host=localhost;dbname=testdb
//連pgsql:
//pgsql:host=localhost port=5432 dbname=testdb user=bruce password=mypass
//連odbc(DSN)
//odbc:testdb
//連access:
//odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\db.mdb;Uid=Admin
//還有oracle,sqlite,db2....
我要執(zhí)行個查詢
foreach ($dbh->query('SELECT * from FOO') as $row) {
print_r($row); //這個結(jié)果和mysql_fetch_array差不多。PDOStatement::setFetchMode 可以調(diào)整。
}
//現(xiàn)在多簡單
另外還可以:
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
得到:
Fetch all of the remaining rows in the result set:
Array
(
[0] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)
[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => pink
)
)
偶還想刪/更新條數(shù)據(jù)。
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
//$count就是刪除的條數(shù)。相當(dāng)于mysql_affected_rows
//也可用PDOStatement::rowCount
偶忘了偶用啥數(shù)據(jù)庫了。。。。
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
echo "Running on mysql; doing something mysql specific here\n";
}
偶插入數(shù)據(jù)的時候要用mysql_escape_string.現(xiàn)在?
print "Unquoted string: $string\n";
print "Quoted string: " . $conn->quote($string) . "\n";
得到:
Unquoted string: Nice
Quoted string: 'Nice'
//你看現(xiàn)在連引號都自動加了。。。。
//注意在不同的數(shù)據(jù)庫中結(jié)果不同,比如有的' => '',有的' => \',\ => \
//現(xiàn)在沒顧慮了,全自動。
//最后偶要關(guān)閉它了
$conn = null; //fcicq和這個數(shù)據(jù)庫連接要說再見了。。。。
//但是!你可以:
$dbh = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2',
array(PDO_ATTR_PERSISTENT => true)); //保持連接
很簡單的不是?
附:特別簡單的特殊調(diào)用方法:
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) { //你怕啥?自動quote!
while ($row = $stmt->fetch()) {
print_r($row);
}
}
也可以:
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
這么好的功能,哪里可以找到?php5.1以上在擴(kuò)展里,php5在pecl里,php4?你別想了,沒有。
聯(lián)系作者fcicq: fcicqbbs at gmail dot com.
如果有問題,或者錯誤,均可聯(lián)系。呵呵。
版權(quán)聲明:非代碼部分(含部分注釋)原創(chuàng),所有代碼的均摘自手冊
最后一句:喜悅首發(fā),嚴(yán)禁轉(zhuǎn)帖
先展望php6(當(dāng)然也可能是php5.2之類,暫未確定)
去掉的東西(感覺有必要提的)
2.1 register_globals
這個影響安全,又不好處理.
2.2 magic_quotes
這個本意很好,但反對的聲音很多...
2.3 safe_mode
這個東西被E_CORE_ERROR代替了.
2.11 register_long_arrays, HTTP_*_VARS
這個東西影響速度
特別的
1. Unicode
這個東西好!不用再擔(dān)心很多了.
3.1 XMLReader / XMLWriter in the distribution, on by default
以后處理rss之類的就更容易了
3.2 Move non-PDO DB extensions to PECL
下面特別討論.
3.3 Move ereg to PECL
實際preg,ereg差不多.用兩個等于浪費.
其余的參見(http://www.php.net/~derick/meeting-notes.html)
============================================
下面描述一下,在php6,沒有mysql_*,我們怎么過.
首先要連接mysql數(shù)據(jù)庫
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
//如果你想連mssql:
//mssql:host=localhost;dbname=testdb
//連pgsql:
//pgsql:host=localhost port=5432 dbname=testdb user=bruce password=mypass
//連odbc(DSN)
//odbc:testdb
//連access:
//odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\db.mdb;Uid=Admin
//還有oracle,sqlite,db2....
我要執(zhí)行個查詢
foreach ($dbh->query('SELECT * from FOO') as $row) {
print_r($row); //這個結(jié)果和mysql_fetch_array差不多。PDOStatement::setFetchMode 可以調(diào)整。
}
//現(xiàn)在多簡單
另外還可以:
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
得到:
Fetch all of the remaining rows in the result set:
Array
(
[0] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)
[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => pink
)
)
偶還想刪/更新條數(shù)據(jù)。
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
//$count就是刪除的條數(shù)。相當(dāng)于mysql_affected_rows
//也可用PDOStatement::rowCount
偶忘了偶用啥數(shù)據(jù)庫了。。。。
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
echo "Running on mysql; doing something mysql specific here\n";
}
偶插入數(shù)據(jù)的時候要用mysql_escape_string.現(xiàn)在?
print "Unquoted string: $string\n";
print "Quoted string: " . $conn->quote($string) . "\n";
得到:
Unquoted string: Nice
Quoted string: 'Nice'
//你看現(xiàn)在連引號都自動加了。。。。
//注意在不同的數(shù)據(jù)庫中結(jié)果不同,比如有的' => '',有的' => \',\ => \
//現(xiàn)在沒顧慮了,全自動。
//最后偶要關(guān)閉它了
$conn = null; //fcicq和這個數(shù)據(jù)庫連接要說再見了。。。。
//但是!你可以:
$dbh = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2',
array(PDO_ATTR_PERSISTENT => true)); //保持連接
很簡單的不是?
附:特別簡單的特殊調(diào)用方法:
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) { //你怕啥?自動quote!
while ($row = $stmt->fetch()) {
print_r($row);
}
}
也可以:
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
這么好的功能,哪里可以找到?php5.1以上在擴(kuò)展里,php5在pecl里,php4?你別想了,沒有。
聯(lián)系作者fcicq: fcicqbbs at gmail dot com.
如果有問題,或者錯誤,均可聯(lián)系。呵呵。
版權(quán)聲明:非代碼部分(含部分注釋)原創(chuàng),所有代碼的均摘自手冊
最后一句:喜悅首發(fā),嚴(yán)禁轉(zhuǎn)帖
※ ※ ※ 本文純屬【游客】個人意見,與【鋼之家鋼鐵博客】立場無關(guān).※ ※ ※
該日志尚無評論! |

該日志尚無評論!



