From 8e6c181c593c2f1e92816643d2c41dd295f71d1e Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Thu, 22 Nov 2018 19:25:30 -0800 Subject: [PATCH] Changed sample code to adhere to PSR standard (#887) --- sample/pdo_sqlsrv_sample.php | 123 +++++++++++++++++------------------ sample/sqlsrv_sample.php | 120 +++++++++++++++++----------------- 2 files changed, 122 insertions(+), 121 deletions(-) diff --git a/sample/pdo_sqlsrv_sample.php b/sample/pdo_sqlsrv_sample.php index 3c92efa3..4c1f61cd 100644 --- a/sample/pdo_sqlsrv_sample.php +++ b/sample/pdo_sqlsrv_sample.php @@ -1,74 +1,73 @@ query( $tsql ); - - //Error handling - FormatErrors ($conn->errorInfo()); - - $productCount = 0; - $ctr = 0; - ?> + $database = "yourdatabase"; + $uid = "yourusername"; + $pwd = "yourpassword"; + + //Establishes the connection + $conn = new PDO("sqlsrv:server=$serverName ; Database = $database", $uid, $pwd); + + //Select Query + $tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; + + //Executes the query + $getProducts = $conn->query($tsql); + + //Error handling + FormatErrors($conn->errorInfo()); + + $productCount = 0; + $ctr = 0; + ?>

First 10 results are :

fetch(PDO::FETCH_ASSOC)) - { - if($ctr>9) - break; - $ctr++; - echo($row['CompanyName']); - echo("
"); - $productCount++; - } - $getProducts = NULL; - - $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.* VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; - - //Insert query - $insertReview = $conn->query( $tsql ); - FormatErrors ($conn->errorInfo()); - ?> + while ($row = $getProducts->fetch(PDO::FETCH_ASSOC)) { + if ($ctr>9) { + break; + } + $ctr++; + echo($row['CompanyName']); + echo("
"); + $productCount++; + } + $getProducts = null; + + $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.* VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; + + //Insert query + $insertReview = $conn->query($tsql); + FormatErrors($conn->errorInfo()); + ?>

Product Key inserted is :

fetch(PDO::FETCH_ASSOC)) - { - echo($row['ProductID']."
"); - } - $insertReview = NULL; - - //Delete Query - //We are deleting the same record - $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; - $param = "SQL New 1"; - - $deleteReview = $conn->prepare($tsql); - $deleteReview->bindParam(1, $param); - - $deleteReview->execute(); - FormatErrors ($deleteReview->errorInfo()); - - function FormatErrors( $error ) - { - /* Display error. */ - echo "Error information:
"; - - echo "SQLSTATE: ".$error[0]."
"; - echo "Code: ".$error[1]."
"; - echo "Message: ".$error[2]."
"; - } + while ($row = $insertReview->fetch(PDO::FETCH_ASSOC)) { + echo($row['ProductID']."
"); + } + $insertReview = null; + + //Delete Query + //We are deleting the same record + $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; + $param = "SQL New 1"; + + $deleteReview = $conn->prepare($tsql); + $deleteReview->bindParam(1, $param); + + $deleteReview->execute(); + FormatErrors($deleteReview->errorInfo()); + + function FormatErrors($error) + { + /* Display error. */ + echo "Error information:
"; + + echo "SQLSTATE: ".$error[0]."
"; + echo "Code: ".$error[1]."
"; + echo "Message: ".$error[2]."
"; + } ?> \ No newline at end of file diff --git a/sample/sqlsrv_sample.php b/sample/sqlsrv_sample.php index 886e06da..db0a0ede 100644 --- a/sample/sqlsrv_sample.php +++ b/sample/sqlsrv_sample.php @@ -2,68 +2,70 @@ echo "\n"; $serverName = "tcp:yourserver.database.windows.net,1433"; $connectionOptions = array("Database"=>"yourdatabase", "Uid"=>"yourusername", "PWD"=>"yourpassword"); - - //Establishes the connection - $conn = sqlsrv_connect($serverName, $connectionOptions); - //Select Query - $tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; - //Executes the query - $getProducts = sqlsrv_query($conn, $tsql); - //Error handling - if ($getProducts == FALSE) - die(FormatErrors(sqlsrv_errors())); - $productCount = 0; - $ctr = 0; - ?> + + //Establishes the connection + $conn = sqlsrv_connect($serverName, $connectionOptions); + //Select Query + $tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; + //Executes the query + $getProducts = sqlsrv_query($conn, $tsql); + //Error handling + if ($getProducts == false) { + die(FormatErrors(sqlsrv_errors())); + } + $productCount = 0; + $ctr = 0; + ?>

First 10 results are :

9) - break; - $ctr++; - echo($row['CompanyName']); - echo("
"); - $productCount++; - } - sqlsrv_free_stmt($getProducts); - - $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; - //Insert query - $insertReview = sqlsrv_query($conn, $tsql); - if($insertReview == FALSE) - die(FormatErrors( sqlsrv_errors())); - ?> + while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) { + if ($ctr>9) { + break; + } + $ctr++; + echo($row['CompanyName']); + echo("
"); + $productCount++; + } + sqlsrv_free_stmt($getProducts); + + $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; + //Insert query + $insertReview = sqlsrv_query($conn, $tsql); + if ($insertReview == false) { + die(FormatErrors(sqlsrv_errors())); + } + ?>

Product Key inserted is :

"; - - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE']."
"; - echo "Code: ".$error['code']."
"; - echo "Message: ".$error['message']."
"; - } - } + while ($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) { + echo($row['ProductID']); + } + sqlsrv_free_stmt($insertReview); + //Delete Query + //We are deleting the same record + $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; + $params = array("SQL New 1"); + + $deleteReview = sqlsrv_prepare($conn, $tsql, $params); + if ($deleteReview == false) { + die(FormatErrors(sqlsrv_errors())); + } + + if (sqlsrv_execute($deleteReview) == false) { + die(FormatErrors(sqlsrv_errors())); + } + + function FormatErrors($errors) + { + /* Display errors. */ + echo "Error information:
"; + + foreach ($errors as $error) { + echo "SQLSTATE: ".$error['SQLSTATE']."
"; + echo "Code: ".$error['code']."
"; + echo "Message: ".$error['message']."
"; + } + } ?> \ No newline at end of file