{"id":4523,"date":"2018-04-30T09:44:48","date_gmt":"2018-04-30T01:44:48","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=4523"},"modified":"2018-07-24T10:02:07","modified_gmt":"2018-07-24T02:02:07","slug":"%e5%9c%a8-windows-%e4%b8%8a-%e4%bd%bf%e7%94%a8-php-pdo-%e9%80%a3%e6%8e%a5-sql-server-2008","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=4523","title":{"rendered":"\u5728 Windows \u4e0a \u4f7f\u7528 PHP PDO \u9023\u63a5 SQL Server 2008"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u5b89\u88dd\u9a45\u52d5\u7a0b\u5f0f Microsoft Driver<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=20098\" target=\"_blank\" rel=\"noopener\">Microsoft Drivers for PHP for SQL Server<\/a> (The Microsoft Drivers 4.0, 3.2, 3.1, and 3.0 for PHP for SQL Server provide connectivity to Microsoft SQL Server from PHP applications.)<\/li>\n<li><a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=55642\" target=\"_blank\" rel=\"noopener\">Microsoft Drivers 4.3 for PHP for SQL Server<\/a><\/li>\n<li><a href=\"https:\/\/www.microsoft.com\/zh-tw\/download\/details.aspx?id=36434\" target=\"_blank\" rel=\"noopener\">Microsoft\u00ae ODBC Driver 11 for SQL Server\u00ae - Windows<\/a><\/li>\n<\/ul>\n<p>\u5728\u6b64\u4f7f\u7528\u4e86 xampp PHP 5.6 \u7248\u672c\uff0c\u56e0\u6b64\u9700\u8981 <strong>3.2 \u7684 sqlsrv driver<\/strong>\uff0c\u4ee5\u53ca <strong>ODBC Driver<\/strong><\/p>\n<p>--<\/p>\n<h2>php.ini<\/h2>\n<p>\u5c07 sqlsrv driver \u89e3\u58d3\u7e2e\u5b89\u88dd\u81f3 c:\\xampp\\php\\ext \u5167\uff0c\u4fee\u6539 php.ini<\/p>\n<pre class=\"lang:default decode:true \">extension=php_pdo_sqlsrv_56_ts.dll\r\nextension=php_sqlsrv_56_ts.dll<\/pre>\n<p>\u4f7f\u7528 <strong>php -m<\/strong> \u6216 <strong>phpinfo()<\/strong> \u6aa2\u67e5\u662f\u5426\u6210\u529f\u8f09\u5165 SQL Server DLL<\/p>\n<p>--<\/p>\n<h2>PHP \u7a0b\u5f0f\u7bc4\u4f8b<\/h2>\n<pre class=\"lang:php decode:true\">&lt;?php\r\n$dsn = 'sqlsrv:server=192.168.1.102;Database=DatabaseName;';\r\n$user = 'username';\r\n$password = 'password';\r\n\r\n\/\/ \u5efa\u7acb PDO \u7269\u4ef6\r\n$pdo = new PDO($dsn, $user, $password);\r\n\r\n$sql = \" SELECT * FROM dbo.TABLE \";\r\n$pre = $pdo-&gt;prepare($sql);\r\n$pre-&gt;execute();\r\n$row = $pre-&gt;fetchAll(2);\r\n\r\nprint_r($row);<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>rowCount() \u7684\u5751<\/h2>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/16531476\/pdo-rowcount-works-on-mysql-but-not-in-sql-server-2008-r2\">PDO rowCount() works on MySQL but not in SQL Server 2008 R2<\/a><\/li>\n<\/ul>\n<p>SQL Server \u6c92\u6709\u56de\u50b3\u6700\u5f8c\u5f71\u97ff\u8cc7\u6599\u6578\u7684\u529f\u80fd\uff0c\u53ea\u80fd\u5be6\u969b\u53bb fetch \u8b80\u53d6\u5f8c\u8a08\u7b97\u5217\u6578\u4f86\u9054\u5230\u76f8\u540c\u529f\u80fd\u9700\u6c42<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"4523\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;23,279&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; -- \u5b89\u88dd\u9a45\u52d5\u7a0b...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"4523\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;23,279&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[260],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4523"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4523"}],"version-history":[{"count":8,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4523\/revisions"}],"predecessor-version":[{"id":4543,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4523\/revisions\/4543"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}