首页 > 解决方案 > 如何使用 Microsoft 提供的 (PDO_SQLSRV) 代码登录以访问 AdventureWorks Production 表?

问题描述

我正在尝试使用 Sql Server AdventureWorks Production Table 和 Microsoft 提供的代码(示例应用程序 (PDO_SQLSRV))为某个项目创建测试应用程序,但是当我运行此代码时,会弹出第二个 2 代码或错误...

{<?php
$serverName  = "OSQSQSql\SQLEXPRESS";
/*Connect using Windows Authentication.*/
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks2017", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r($e->getMessage() ) );

相反,我得到了这个回应 [2]

   { setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(Exception $e) { die( print_r($e->getMessage() ) ); } if(isset($_REQUEST['action'])) { switch( $_REQUEST['action'] ) { /* Get AdventureWorks products by querying against the product name*/ case 'getproducts': try { $params = array($_POST['query']); $tsql = "SELECT ProductID, Name, Color, Size, ListPrice FROM Production.Product WHERE Name LIKE '%' + ? + '%' AND ListPrice > 0.0"; $getProducts = $conn->prepare($tsql); $getProducts->execute($params); $products = $getProducts->fetchAll(PDO::FETCH_ASSOC); $productsCount = count($products); if($productsCount > 0) { BeginProductsTable($productCount); foreach( $products as $row ) { PopulateProductsTable( $row ); } EndProductsTable(); } else { DisplayNoProductsMsg(); } } catch(Exception $e) { die(print_r( $e->getMessage() ) ); } GetSeachTerms( !null ); break; /* Get reviews for a specified productID.*/ case 'getreview': GetPicture($_GET['productid'] ); break; /* Write a review for a specified productID.*/ case 'writereview': DisplayWriteReviewForm( $_POST['productid'] ); break; /* Submit a review to the database.*/ case 'submitreview': try { $tsql = "INSERT INTO Production.ProductReview (ProductID, ReviewerName, ReviewDate, EmailAddress, Rating, Comments) VALUES (?,?,?,?,?,?)"; $params = array(&$_POST['procductid'], &$_POST['name'], date("Y-m-d"), &$_POST['email'], &$_POST['rating'], &$_POST['comments']); $insertReview = $conn->prepare($tsql); $insertReview->execute($params); } catch(Exception $e) { die( print_r( $e->getMessage() ) ); } GetSeachTerms( true ); GetReviews( $conn, $_POST['productid'] ); break; /* Display form for uploading a picture.*/ case 'displayuploadpictureform': try { $tsql = "SELECT Name FROM Production.Product WHERE ProductID = ?"; $getName = $conn->prepare($tsql); $getName->execute(array($_GET['productid'])); $name = $getName->fetchColumn(0); } catch(Exception $e) { die(print_r( $e->getMessage() ) ); } DisplayUploadPictureForm( $_GET['procductid, $name ); break; /* Upload a new picturefor the selected product.*/ case 'uploadpicture'; try { $tsql = "INSERT INTO Production.ProductPhoto (LargePhoto) VALUES (?)"; $uploadPic = $conn->prepare($tsql); $fileStream = fopen($_FILES['file']['tmp_name'], "r"); $uploadPic->bindParam(1, $fileStream, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); $uploadPic->execute(); /* Get the first field - the identity from INSERT - so we can associate it with the product ID. */ $photoID = $conn->lastInsertid(); $tsql = "UPDATE Production.ProductProductPhoto SET ProductPhotoID = ? WHERE ProductID = ?"; $associateids->execute(array($photoID, $_POST['productid'])); } catch(Exception $e) { die(print_r($e->getMessage())); } GetPicture( $_POST['productid']); DisplayWriteReviewButton( $_POST['productid'] ); GetSeachTerms (!null); break; }//End Switch } else { GetSeachTerms( !null ); } function GetPicture( $productID ); { echo ""; echo ""; } function GetReviews( $conn, $producID ) { try { $tsql = "SELECT ReviewerName, CONVERT(verchar(32), ReviewDate, 107, AS [ReviewDate], Rating, Comments FROM ProductID = ? ORDER BY ReviewDate DESC"; $getReviews = $conn->prepare( $tsql); $getReviews->execute(array($productID)); $review = $getReviews->fetchAll(PDO::FETCH_NUM); $reviewCount = count($reviews); if($reviewCount > 0 ) { foreach($reviews as $row) { $name = $row[0]; $date = $row[1]; $rating = $row[2]; $comments = $row[3]; DisplayReview( $productID, $name, $date, $rating, $comments ); } } else { DisplayNoReviewMsg(); } } catch(Exception $e) { die(print_r($e->getMessage())); } DisplayWriteReviewButton( $producID ); GetSearchTerms(!null); } /*** Presentation and Utility Functions ***/ function BeginProductsTable($rowCount) { /* Display the beginning of the search result table. */ $headings = array("Product ID" , "Product Name", "Color", "Size", "Price"); echo "}```

请问我在这里做错了什么,任何建议,使用 PDO_SQLSRV 创建示例应用程序的正确测试代码是什么?

标签: phpsql-serverpdo

解决方案


问题是,我在根文件中创建了文件夹并像这样在该文件夹中添加文件SqlApp/adventureworks_demo.php,我通过从文件夹中删除文件并将文件直接放入 xampp root/ 来解决问题,adventureworks_demo.php就像 Microsoft 指示的那样。

所以不要将文件放在新创建的文件夹中,只需将文件直接保存到根文件...


推荐阅读