Home >Backend Development >PHP Tutorial >PHP涓瓓DO鄄勪媔鐢ㄦ南娉曟€鍤粨_PHP鏁欑▼ | 宁涔嫔

PHP涓瓓DO鄄勪媔鐢ㄦ南娉曟€鍤粨_PHP鏁欑▼ | 宁涔嫔

WBOY
WBOYOriginal
2016-07-13 17:49:326694browse

The chain is open to the public.

寮€揮嬫嬯阃犵涓€涓狿DO搴洴敤曛屽湪Web揂囨.鐩綍涓嫔山夤 Hong竴涓猵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锛忟隟鎹簮锛夛纴read鄄愄鋄勪jun鎭set鎷fine edition of 嵁搴撶 being coaxed by the 鍨嬫槸槸mysql锛屼 wealthy chain鹴鍧€鏄localhost锛屾暟鎹簱钖皞О鏄綶est锛屽NH杩欎箞獑犱釜奇℃伅銆备笉钖屾暟鎹簱鄄勬暟鎹簮鏋勯€銳寮忔槧涓觴What's wrong with the wind?/p>

$db = new PDO($dsn, 'root', '');
鍒濆鍖栦竴涓狿DO瀵 silicon thin layer簩涓槸杩炴帴鏁版嵁搴洴湇锷″洒鄄勭椴鴴鴴紴紗笁涓嬤鏁版槸瀵嗿爜銆?/p>

$count = $db->exec("INSERT INTO foo SET name = 'lix',gender='mail',time=NOW()");
echo $count;
璋卂敤杩炴崴鎴愬姛鄄凯DO瀵珉珉形獏屼竴涓璇纴杩掎釜镆ヨ鏄竴涓彃鍏ヤ竴鏉¤褰kuang殑鎎鎎The execution of DO::exec() is done by executing DO::exec() Pu¤祫婧愶丝
$db = null;
Hazelnuts Jibangxiarray( PDO::ATTR_PERSISTENT => true) $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);
}
?>
涓€娆¤叏鍙栨徍chain勋暟鎹?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] =>                                                                                             [1] =>                                                                                                           [gender] => 鐢?nbsp;
                                              [2] => 鐢?nbsp;
                                                                                                                                                                                                                                                      ​                                          [3] => 2006-10-28 23:14:23
)
}
掴戜 Slide 鐪嬮嬷闱㈢殑璁 Board綍锛屾暟瀛楮曞拰鍏 fond 仈绱㈠紩鮮 Mustard 湁锛屾 nitrogen lu silicon 祫婧愶纴鴴戜 slippery鍙渶Jijibo flag Chu Liao is open and ruined?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);
?>
settattribute () 鏂 硶鏄    灞炴 € € 纴 纴   灞炴 € 湁锛 ф ф ф 湁锛 € € ф ф € ф ф € € ф ф ф ф € ф ф € 湁锛 € 湁锛 € _ _ DO :: Attr_errmode 夌 屾 垜浠 垜浠 垜浠 岄渶 岄渶 岄渶 岄渶 岄渶 岄渶DO::ATTR_CASEだ紩鏄ぇ鍐澾缮鏄皬鍐欙纴chain夊嚑涓€夋嫨锛?br /> PDO::CASE_LOWER -- What's going on?br /> PDO::CASE_NATURAL- PDO::CASE_UPPER -- liao coax bu 卒楀敕涓 coax ぇ鍐?br /> setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode setFetchMode 🎜> PDO::FETCH_ASSOC -- 鏏 Chong Ba Jing plays the role of 粍褰㈠Fang
PDO::FETCH_NUM -- 鏁板秧绱㈠紩鏁 Dressing up the rice 褰㈠Fang
PDO::FETCH_BOTH -- 涓よ€呮暟缁勫毮忛兘chain夛纴杩欐槂悂 Hong渷镄?br /> PDO::FETCH_OBJ -- FETCH_OBJ mysql_fetch_object()

褰撶姧锛屼竴鑸叏鍐典笅鴴戜 slip鏄Jiao鐢≒DO::FETCH_ASSOC锛屽叿Huanpujiao鐢ㄤ綈涬嬬纴鎸夌Receive Huan纜嚜宸讑黑€ Xia侊纴鍏多粬髮峰彇竫诲瀷鍙卝€冩drop鍐屻€?/p>

闄や简涓婇综合杩欑隮峰彇鏁版偁镄勬南寮庴杩华彲浠ワ fine

[php]
$rs = $db->prepare("SELECT * FROM foo");
$rs->execute();
while($row = $rs->fetch()){
print_r($row);
}
?>
" [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()鏉ユ墽琛岄澶勭悊悊悊岨"櫧鐨勶紝澶у鍙 mutual 鍙傝€冩坠鍐屽拰鍏朵粬鏂囨.銆?/p> DOStatement::fetchColumn()銆丳DOStatement::fetch()銆丳DOStatement::fetchALL()銆侾DOStatement:()鏄幏鍙栫粨鏋沧寚瀹氱涓€鏉¤褰旷殑鏌愪釜瀛楁锛岀己鐪佹槸绗竴涓点瓧: 佹槸绗竴涓point瓧: 佹槸绗竴涓点瓧敤鏉ヨ幏鍙栦竴鏉¤褰曪紝PDOStatement::fetchAll()鏄幏鍙栨墍颍链夎褰曢泦鍒颁竴涓练竔经獙竏獙竴竏獙练竔经獙练竔竴獙绠竴竴竴竴竴竴竴竴竴竴竴竴竴鍙鍙鍙鍙鍙栦髴鏉¤褰曪紝PDOStatement彲浠ラ€氳PDOStatement::setFetchMode 鏉ヨ缃渶瑕佺粨鏋滈泦鍚堢殑绫诲瀷銆?/p> PDO::lastInsertId()鍜孭DOStatement::rowCount()銆侾DO::: ()鍥炰笂Rao℃彃鍏ユ搷搷滐紝涓婚敭鍒楃被鍨嬫槸鑷鐨勬渶鍚庣殑鑷ID銆侶DOStatement: 銆侶DOStatement: 劆侶 DOStatement: 銆侶 DOStatement ::query()鍜孭DO::prepare()杩涜DELETE銆両NSERT銆乁PDATE鎿崭綔褰卞搷鐨勭粨鏋滈泦锛屽鏋滈泦锛屽PDO::exec()钏锶What's going on?br /> 銆愰敊璇鐞嗐€?br /> 1. 闈㈠悜瀵Silicon Thin 勬柟Liao弚www.2cto.com
鍏堢湅鐪嫔鏋滆繛鎺ラ敊璇瓑鐨勫鐞嗭紝浣跨敤闈㈠悜瀵硅薄鐨勬柟寮忔潵潵澶澶澶澶

[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鏉ュ垵寲涓€涓Brother Shuying was 銆?br /> PDOException 寮傚父绫灞炴€х粨鏋勶小
[php]
class PDOException extends Exception
{
public $errorInfo = null; // 閿栾淇℃伅锛屽彲浠ヨ皟鐢?PDO::errorInfo()鎴?PDOStatement::errorInfo()鏉ヨ闂?nbsp; protected $message; // 寮傚父淇℃伅锛屽彲浠ヨ瘯鐢?Exception::getMessage() 鏉ヨ闂?nbsp;
protected $code; // SQL bell rudder €侀敊璇bluff sickle 侊紝鍙mutually swept across Exception::getCode() 鏉ヨ闂?nbsp;
}
?>
绠€鍗旷殑鐪嬩竴涓婸HP 5鍐呯疆鐨勫滠嗙被缁撴瀯锛?br /> [php]
class Exception
{
// 灞炴€?nbsp;
protected $message = 'Unknown exception'; // Liao's father Qi ℃伅
protected $code = 0; // 鐢ㄦ埛鑷畾涔夊畾畾椊杪靥?nbsp;
protected $file; // 鍙戠敓liao傚father 勬枃浠 stomping
protected $line;
// 鏂gui綶
final function getMessage(); Final function getCode(); Final function getFile(); final function getLine(); final function getTrace(); // backtrace() 鏁衣粍
final function getTraceAsString(); // GetTrace() getTrace()
}
?>
Draft 勶纴鍦 ㄤ 佷 佷 鍙 钖 堥 € 傜殑 傜殑 傜殑 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 o Dostatement 瀵 silicon thin chain 塭 RRORCODE () 鍜? Errorinfo () ErrorInfo()拋MySQL鈥勯敊璇抙抸簸昌澾Q℃伅锛屾暟缁拭粨鋋勫涓嬶精品
[php]
Array
(
[0] => 42S22
[1] => 1054
[2] => Unknown column 'aaa' in 'field list'
)
Error Code() Error Code()槗镊fanxiu厗阌澾奇℃伅鏄剧ず銆?br />
鎽樿嚜娌 Chongぇ鏉庝君镄?Crazy Coding浜红敓




http://www.bkjia.com/PHPjc/478348.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/478348.htmlTechArticlechain粨涓€涓婸DO鄄勪媽吢ㄣ€?寮€pu嬫嬯阃犵涓€涓狿DO搴搴畤锛屽湪Web揂囨.鐩綍涓嫔山夤 Hong竴涓猵do.php鏂囦Huan锛?[php] ?php $dsn = mysql:hos...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn