Heim > Backend-Entwicklung > PHP-Tutorial > PHP涓璓DO鐨勪娇鐢ㄦ柟娉曟€荤粨_PHP鏁欑▼ | 甯涔嬪

PHP涓璓DO鐨勪娇鐢ㄦ柟娉曟€荤粨_PHP鏁欑▼ | 甯涔嬪

WBOY
Freigeben: 2016-07-13 17:49:32
Original
6705 Leute haben es durchsucht

鏈枃涓嶆秹鍙奝DO鐨勫畨瑁咃紝鍙槸绠€鍗曟€荤粨涓€涓婸DO鐨勪娇鐢ㄣ€?/p>

寮€濮嬫瀯閫犵涓€涓狿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浜虹敓

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478348.htmlTechArticle鏈枃涓嶆秹鍙奝DO鐨勫畨瑁咃紝鍙槸绠€鍗曟€荤粨涓€涓婸DO鐨勪娇鐢ㄣ€?寮€濮嬫瀯閫犵涓€涓狿DO搴旂敤锛屽湪Web鏂囨。鐩綍涓嬪垱寤轰竴涓猵do.php鏂囦欢锛?[php] ?php $dsn = mysql:hos...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage