时间:2021-07-01 10:21:17 帮助过:5人阅读
寮€濮嬫瀯閫犵涓€涓狿DO搴旂敤锛屽湪Web鏂囨。鐩綍涓嬪垱寤轰竴涓猵do.php鏂囦欢锛?/p>
[php]
$dsn = "mysql:host=localhost;dbname=test";
$db = new PDO($dsn, 'root', '');
$count = $db->exec("INSERT INTO foo SET name = 'lix',gender='mail',time=NOW()");
echo $count;
$db = null;
?>
$dsn = "mysql:host=localhost;dbname=test";
鐢ㄦ潵鏋勯€燚SN锛堟暟鎹簮锛夛紝閲岄潰鐨勪俊鎭寘鎷細鏁版嵁搴撶被鍨嬫槸mysql锛屼富鏈哄湴鍧€鏄痩ocalhost锛屾暟鎹簱鍚嶇О鏄痶est锛屽氨杩欎箞鍑犱釜淇℃伅銆備笉鍚屾暟鎹簱鐨勬暟鎹簮鏋勯€犳柟寮忔槸涓嶄竴鏍风殑銆?/p>
$db = new PDO($dsn, 'root', '');
鍒濆鍖栦竴涓狿DO瀵硅薄锛屾瀯閫犲嚱鏁扮殑鍙傛暟绗竴涓氨鏄垜浠殑鏁版嵁婧愶紝绗簩涓槸杩炴帴鏁版嵁搴撴湇鍔″櫒鐨勭敤鎴凤紝绗笁涓弬鏁版槸瀵嗙爜銆?/p>
$count = $db->exec("INSERT INTO foo SET name = 'lix',gender='mail',time=NOW()");
echo $count;
璋冪敤杩炴帴鎴愬姛鐨凱DO瀵硅薄鏉ユ墽琛屼竴涓煡璇紝杩欎釜鏌ヨ鏄竴涓彃鍏ヤ竴鏉¤褰曠殑鎿嶄綔锛屼娇鐢≒DO::exec() 鏂规硶浼氳繑鍥炰竴涓奖鍝嶈褰曠殑缁撴灉銆傛渶鍚庤繕鏄渶瑕佺粨鏉熷璞¤祫婧愶細
$db = null;
榛樿杩欎釜涓嶆槸闀胯繛鎺ワ紝濡傛灉闇€瑕佹暟鎹簱闀胯繛鎺ワ紝闇€瑕佹渶鍚庡姞涓€涓弬鏁帮細array(PDO::ATTR_PERSISTENT => true) 鍙樻垚杩欐牱锛?br />
$db = new PDO($dsn, 'root', '', array(PDO::ATTR_PERSISTENT => true));
濡傛灉鎯宠幏鍙栨暟鎹?br />
[php]
$rs=$db->query("SELECT * FROM foo");
foreach($rs->fetch() as $row){
print_r($row);
}
?>
涔熷彲浠ョ敤
[php]
$rs = $db->query("SELECT * FROM foo");
while($row = $rs->fetch()){
print_r($row);
}
?>
涓€娆¤幏鍙栨墍鏈夋暟鎹?br />
[php]
$rs = $db->query("SELECT * FROM foo");
$result_arr = $rs->fetchAll();
print_r($result_arr);
?>
缁撴灉鏄剧ず锛?br />
[php]
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[name] => heiyeluren
[1] => heiyeluren
[gender] => 鐢?nbsp;
[2] => 鐢?nbsp;
[time] => 2006-10-28 23:14:23
[3] => 2006-10-28 23:14:23
)
}
鎴戜滑鐪嬮噷闈㈢殑璁板綍锛屾暟瀛楃储寮曞拰鍏宠仈绱㈠紩閮芥湁锛屾氮璐硅祫婧愶紝鎴戜滑鍙渶瑕佸叧鑱旂储寮曠殑锛?br />
[php]
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
$rs = $db->query("SELECT * FROM foo");
$rs->setFetchMode(PDO::FETCH_ASSOC);
$result_arr = $rs->fetchAll();
print_r($result_arr);
?>
setAttribute() 鏂规硶鏄缃甈DO灞炴€э紝涓昏灞炴€ф湁锛歅DO::ATTR_CASE銆丳DO::ATTR_ERRMODE绛夌瓑锛屾垜浠繖閲岄渶瑕佽缃殑鏄疨DO::ATTR_CASE锛屽氨鏄垜浠娇鐢ㄥ叧鑱旂储寮曡幏鍙栨暟鎹泦鐨勬椂鍊欙紝鍏宠仈绱㈠紩鏄ぇ鍐欒繕鏄皬鍐欙紝鏈夊嚑涓€夋嫨锛?br />
PDO::CASE_LOWER -- 寮哄埗鍒楀悕鏄皬鍐?br />
PDO::CASE_NATURAL -- 鍒楀悕鎸夌収鍘熷鐨勬柟寮?br />
PDO::CASE_UPPER -- 寮哄埗鍒楀悕涓哄ぇ鍐?br />
鎴戜滑浣跨敤setFetchMode鏂规硶鏉ヨ缃幏鍙栫粨鏋滈泦鐨勮繑鍥炲€肩殑绫诲瀷锛屽悓鏍风被鍨嬭繕鏈夛細
PDO::FETCH_ASSOC -- 鍏宠仈鏁扮粍褰㈠紡
PDO::FETCH_NUM -- 鏁板瓧绱㈠紩鏁扮粍褰㈠紡
PDO::FETCH_BOTH -- 涓よ€呮暟缁勫舰寮忛兘鏈夛紝杩欐槸缂虹渷鐨?br />
PDO::FETCH_OBJ -- 鎸夌収瀵硅薄鐨勫舰寮忥紝绫讳技浜庝互鍓嶇殑 mysql_fetch_object()
褰撶劧锛屼竴鑸儏鍐典笅鎴戜滑鏄娇鐢≒DO::FETCH_ASSOC锛屽叿浣撲娇鐢ㄤ粈涔堬紝鎸夌収浣犺嚜宸辩殑闇€瑕侊紝鍏朵粬鑾峰彇绫诲瀷鍙傝€冩墜鍐屻€?/p>
闄や簡涓婇潰杩欑鑾峰彇鏁版嵁鐨勬柟寮忥紝杩樺彲浠ワ細
[php]
$rs = $db->prepare("SELECT * FROM foo");
$rs->execute();
while($row = $rs->fetch()){
print_r($row);
}
?>
濡傛灉浣犳兂鑾峰彇鎸囧畾璁板綍閲屼竴涓瓧娈电粨鏋滅殑璇濓紝鍙互浣跨敤 PDOStatement::fetchColumn():
[php]
$rs = $db->query("SELECT COUNT(*) FROM foo");
$col = $rs->fetchColumn();
echo $col;
?>
灏忕粨锛?br />
鏌ヨ鎿嶄綔涓昏鏄疨DO::query()銆丳DO::exec()銆丳DO::prepare()銆侾DO::query()涓昏鏄敤浜庢湁璁板綍缁撴灉杩斿洖鐨勬搷浣滐紝鐗瑰埆鏄疭ELECT鎿嶄綔锛孭DO::exec()涓昏鏄拡瀵规病鏈夌粨鏋滈泦鍚堣繑鍥炵殑鎿嶄綔锛屾瘮濡侷NSERT銆乁PDATE銆丏ELETE绛夋搷浣滐紝瀹冭繑鍥炵殑缁撴灉鏄綋鍓嶆搷浣滃奖鍝嶇殑鍒楁暟銆侾DO::prepare()涓昏鏄澶勭悊鎿嶄綔锛岄渶瑕侀€氳繃$rs->execute()鏉ユ墽琛岄澶勭悊閲岄潰鐨凷QL璇彞锛岃繖涓柟娉曞彲浠ョ粦瀹氬弬鏁帮紝鍔熻兘姣旇緝寮哄ぇ锛屼笉鏄湰鏂囪兘澶熺畝鍗曡鏄庣櫧鐨勶紝澶у鍙互鍙傝€冩墜鍐屽拰鍏朵粬鏂囨。銆?/p>
鑾峰彇缁撴灉闆嗘搷浣滀富瑕佹槸锛歅DOStatement::fetchColumn()銆丳DOStatement::fetch()銆丳DOStatement::fetchALL()銆侾DOStatement::fetchColumn() 鏄幏鍙栫粨鏋滄寚瀹氱涓€鏉¤褰曠殑鏌愪釜瀛楁锛岀己鐪佹槸绗竴涓瓧娈点€侾DOStatement::fetch() 鏄敤鏉ヨ幏鍙栦竴鏉¤褰曪紝PDOStatement::fetchAll()鏄幏鍙栨墍鏈夎褰曢泦鍒颁竴涓腑锛岃幏鍙栫粨鏋滃彲浠ラ€氳繃PDOStatement::setFetchMode鏉ヨ缃渶瑕佺粨鏋滈泦鍚堢殑绫诲瀷銆?/p>
鍙﹀鏈変袱涓懆杈圭殑鎿嶄綔锛屼竴涓槸PDO::lastInsertId()鍜孭DOStatement::rowCount()銆侾DO::lastInsertId()鏄繑鍥炰笂娆℃彃鍏ユ搷浣滐紝涓婚敭鍒楃被鍨嬫槸鑷鐨勬渶鍚庣殑鑷ID銆侾DOStatement::rowCount()涓昏鏄敤浜嶱DO::query()鍜孭DO::prepare()杩涜DELETE銆両NSERT銆乁PDATE鎿嶄綔褰卞搷鐨勭粨鏋滈泦锛屽PDO::exec()鏂规硶鍜孲ELECT鎿嶄綔鏃犳晥銆?br />
銆愰敊璇鐞嗐€?br />
1. 闈㈠悜瀵硅薄鐨勬柟寮弚ww.2cto.com
鍏堢湅鐪嬪鏋滆繛鎺ラ敊璇瓑鐨勫鐞嗭紝浣跨敤闈㈠悜瀵硅薄鐨勬柟寮忔潵澶勭悊锛?/p>
[php]
try {
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$db = null;
} catch (PDOException $e) {
print "Error: " . $e->getMessage() . "
";
die();
}
?>
杩欓噷鍒╃敤鎴戜滑PHP 5闈㈠悜瀵硅薄鐨勫紓甯稿鐞嗙壒寰侊紝濡傛灉閲岄潰鏈夊紓甯哥殑璇濆氨鍒濆鍖栬皟鐢≒DOException鏉ュ垵濮嬪寲涓€涓紓甯哥被銆?br />
PDOException寮傚父绫荤殑灞炴€х粨鏋勶細
[php]
class PDOException extends Exception
{
public $errorInfo = null; // 閿欒淇℃伅锛屽彲浠ヨ皟鐢?PDO::errorInfo() 鎴?PDOStatement::errorInfo()鏉ヨ闂?nbsp;
protected $message; // 寮傚父淇℃伅锛屽彲浠ヨ瘯鐢?Exception::getMessage() 鏉ヨ闂?nbsp;
protected $code; // SQL鐘舵€侀敊璇唬鐮侊紝鍙互浣跨敤 Exception::getCode() 鏉ヨ闂?nbsp;
}
?>
绠€鍗曠殑鐪嬩竴涓婸HP 5鍐呯疆鐨勫紓甯稿鐞嗙被缁撴瀯锛?br />
[php]
class Exception
{
// 灞炴€?nbsp;
protected $message = 'Unknown exception'; // 寮傚父淇℃伅
protected $code = 0; // 鐢ㄦ埛鑷畾涔夊紓甯镐唬鐮?nbsp;
protected $file; // 鍙戠敓寮傚父鐨勬枃浠跺悕
protected $line; // 鍙戠敓寮傚父鐨勪唬鐮佽鍙?nbsp;
// 鏂规硶
final function getMessage(); // 杩斿洖寮傚父淇℃伅
final function getCode(); // 杩斿洖寮傚父浠g爜
final function getFile(); // 杩斿洖鍙戠敓寮傚父鐨勬枃浠跺悕
final function getLine(); // 杩斿洖鍙戠敓寮傚父鐨勪唬鐮佽鍙?nbsp;
final function getTrace(); // backtrace() 鏁扮粍
final function getTraceAsString(); // 宸叉牸鎴愬寲鎴愬瓧绗︿覆鐨?getTrace() 淇℃伅
}
?>
鐩稿簲鐨勶紝鍦ㄤ唬鐮佷腑鍙互鍚堥€傜殑璋冪敤 getFile() 鍜?getLine() 鏉ヨ繘琛岄敊璇畾浣嶏紝鏇存柟渚跨殑杩涜璋冭瘯銆?br />
2. 闈㈠悜杩囩▼鐨勬柟娉?br />
[php]
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$rs = $db->query("SELECT aa,bb,cc FROM foo");
if ($db->errorCode() != '00000'){
print_r($db->errorInfo());
exit;
}
$arr = $rs->fetchAll();
print_r($arr);
$db = null;
?>
PDO鍜孭DOStatement瀵硅薄鏈塭rrorCode() 鍜?errorInfo() 鏂规硶锛屽鏋滄病鏈変换浣曢敊璇? errorCode() 杩斿洖鐨勬槸: 00000 锛屽惁鍒欏氨浼氳繑鍥炰竴浜涢敊璇唬鐮併€俥rrorInfo() 杩斿洖鐨勪竴涓暟缁勶紝鍖呮嫭PHP瀹氫箟鐨勯敊璇唬鐮佸拰MySQL鐨勯敊璇唬鐮佸拰閿欒淇℃伅锛屾暟缁勭粨鏋勫涓嬶細
[php]
Array
(
[0] => 42S22
[1] => 1054
[2] => Unknown column 'aaa' in 'field list'
)
姣忔鎵ц鏌ヨ浠ュ悗锛宔rrorCode() 鐨勭粨鏋滈兘鏄渶鏂扮殑锛屾墍浠ユ垜浠彲浠ュ緢瀹规槗鑷繁鎺у埗閿欒淇℃伅鏄剧ず銆?br />
鎽樿嚜 娌冲ぇ鏉庝俊鐨?Crazy Coding浜虹敓
http://www.bkjia.com/PHPjc/478348.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478348.htmlTechArticle鏈枃涓嶆秹鍙奝DO鐨勫畨瑁咃紝鍙槸绠€鍗曟€荤粨涓€涓婸DO鐨勪娇鐢ㄣ€?寮€濮嬫瀯閫犵涓€涓狿DO搴旂敤锛屽湪Web鏂囨。鐩綍涓嬪垱寤轰竴涓猵do.php鏂囦欢锛?[php] ?php $dsn = mysql:hos...