{"id":898,"date":"2021-03-08T01:02:04","date_gmt":"2021-03-07T17:02:04","guid":{"rendered":"http:\/\/www.tra56.com\/?p=898"},"modified":"2021-03-08T01:02:04","modified_gmt":"2021-03-07T17:02:04","slug":"898","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/898.html","title":{"rendered":"PHP\u4f7f\u7528PDO\u5b9e\u73b0\u7b80\u5355\u7684\u589e\u5220\u6539\u67e5"},"content":{"rendered":"
<?php\r\n\/\/\/\/\/php\u64cd\u4f5cpdo\u5b9e\u73b0\u67e5\u8be2\r\nheader( "Content-type: text\/html; charset=utf-8" );\r\n$dbms = 'mysql';\r\n$user = 'root';\r\n$pwd = '12345678';\r\n$dbName = 'ceshi';\r\n$host = 'localhost';\r\n$charset = 'utf8';\r\n$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";\r\ntry {\r\n$pdo = new PDO( $dsn, $user, $pwd );\r\n} catch ( Exception $e ) {\r\necho $e->getMessage();\r\n}\r\n\/\/\u67e5\u8be2\r\n$sql = "select * from dunling_chat where id=? ";\r\n\/\/\u51c6\u5907sql\u6a21\u677f\r\n$stmt = $pdo->prepare( $sql );\r\n$id = '1';\r\n\/\/\u7ed1\u5b9a\u53c2\u6570\r\n$stmt->bindValue( 1, $id );\r\n\/\/\u6267\u884c\u9884\u5904\u7406\u8bed\u53e5\r\n$stmt->execute();\r\n\/\/\u63a8\u8350\u8fd9\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\r\nwhile ( $row = $stmt->fetch() ) {\r\necho $row[ 'id' ] . "<br \/>";\r\necho $row[ 'nicheng' ] . "<br \/>";\r\necho $row[ 'content' ] . "<br \/>";\r\necho $row[ 'time' ] . "<br \/>";\r\n}\r\n\/\/\u91ca\u653e\u67e5\u8be2\u7ed3\u679c\r\n$stmt = null;\r\n\/\/\u5173\u95ed\u8fde\u63a5\r\n$pdo = null;\r\n?>\r\n \r\n \r\n \r\n<?php\r\n\/\/\/\/\/php\u64cd\u4f5cpdo\u5b9e\u73b0\u66f4\u65b0\r\nheader( "Content-type: text\/html; charset=utf-8" );\r\n$dbms = 'mysql';\r\n$user = 'root';\r\n$pwd = '12345678';\r\n$dbName = 'ceshi';\r\n$host = 'localhost';\r\n$charset = 'utf8';\r\n$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";\r\ntry {\r\n$pdo = new PDO( $dsn, $user, $pwd );\r\n} catch ( Exception $e ) {\r\necho $e->getMessage();\r\n}\r\n\/\/\u66f4\u65b0\r\n$sql = "update dunling_chat set nicheng=? where id=?";\r\n\/\/\u51c6\u5907sql\u6a21\u677f\r\n$stmt = $pdo->prepare( $sql );\r\n$name = 'one';\r\n$age = 1;\r\n\/\/\u7ed1\u5b9a\u53c2\u6570\r\n$stmt->bindValue( 1, $name );\r\n$stmt->bindValue( 2, $age );\r\n\/\/\u6267\u884c\u9884\u5904\u7406\u8bed\u53e5\r\n$stmt->execute();\r\n$affect_row = $stmt->rowCount();\r\nif ( $affect_row ) {\r\necho '\u66f4\u65b0\u6210\u529f' . '<br>';\r\n} else {\r\necho '\u66f4\u65b0\u5931\u8d25' . '<br>';\r\n}\r\n\/\/\u91ca\u653e\u67e5\u8be2\u7ed3\u679c\r\n$stmt = null;\r\n\/\/\u5173\u95ed\u8fde\u63a5\r\n$pdo = null;\r\n?>\r\n \r\n \r\n<?php\r\n\/\/\/\/php\u64cd\u4f5cpdo\u5b9e\u73b0\u63d2\u5165\r\n$dbms = 'mysql';\r\n$user = 'root';\r\n$pwd = '12345678';\r\n$dbName = 'ceshi';\r\n$host = 'localhost';\r\n$charset = 'utf8';\r\n$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";\r\ntry {\r\n$pdo = new PDO( $dsn, $user, $pwd );\r\n} catch ( Exception $e ) {\r\necho $e->getMessage();\r\n}\r\n\/\/\u63d2\u5165\r\n$sql = "insert into dunling_chat(nicheng,content) values(?,?)";\r\n\/\/\u51c6\u5907sql\u6a21\u677f\r\n$stmt = $pdo->prepare( $sql );\r\n$nicheng = 'two';\r\n$content = 000;\r\n\/\/\u7ed1\u5b9a\u53c2\u6570\r\n$stmt->bindValue( 1, $nicheng );\r\n$stmt->bindValue( 2, $content );\r\n\/\/\u6267\u884c\u9884\u5904\u7406\u8bed\u53e5\r\n$stmt->execute();\r\n$insert_id = $pdo->lastInsertId();\r\nif ( $insert_id ) {\r\necho '\u65b0\u589e\u6210\u529f' . '<br>';\r\n} else {\r\necho '\u65b0\u589e\u5931\u8d25' . '<br>';\r\n}\r\n\/\/\u91ca\u653e\u67e5\u8be2\u7ed3\u679c\r\n$stmt = null;\r\n\/\/\u5173\u95ed\u8fde\u63a5\r\n$pdo = null;\r\n?>\r\n \r\n \r\n<?php\r\n\r\n\/\/\/\/php\u64cd\u4f5cpdo\u5b9e\u73b0\u5220\u9664\r\nheader("Content-type: text\/html; charset=utf-8");\r\n$dbms = 'mysql';\r\n$user = 'root';\r\n$pwd = '12345678';\r\n$dbName = 'ceshi';\r\n$host = 'localhost';\r\n$charset = 'utf8';\r\n$dsn="$dbms:host=$host;dbname=$dbName;charset=$charset";\r\ntry{\r\n$pdo=new PDO($dsn,$user,$pwd);\r\n}\r\ncatch(Exception $e)\r\n{\r\necho $e->getMessage();\r\n}\r\n\r\n\/\/\u5220\u9664\r\n$sql = "delete from dunling_chat where id=?";\r\n\/\/\u51c6\u5907sql\u6a21\u677f\r\n$stmt = $pdo->prepare($sql);\r\n$id = 11;\r\n\/\/\u7ed1\u5b9a\u53c2\u6570\r\n$stmt->bindValue(1,$id);\r\n\/\/\u6267\u884c\u9884\u5904\u7406\u8bed\u53e5\r\n$stmt->execute();\r\n$affect_row = $stmt->rowCount();\r\nif($affect_row)\r\n{\r\necho '\u5220\u9664\u6210\u529f'.'<br>';\r\n}\r\nelse\r\n{\r\necho '\u5220\u9664\u5931\u8d25'.'<br>';\r\n}\r\n\/\/\u91ca\u653e\u67e5\u8be2\u7ed3\u679c\r\n$stmt = null;\r\n\/\/\u5173\u95ed\u8fde\u63a5\r\n$pdo = null;\r\n?><\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"
<?php \/\/\/\/\/php\u64cd\u4f5cpdo\u5b9e\u73b0\u67e5\u8be2 header( "Content-t […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[26,72,70],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/898"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/898\/revisions\/899"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}