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

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

Jul 13, 2016 pm 05:49 PM
pdo php

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles