Changed sample code to adhere to PSR standard (#887)

This commit is contained in:
Jenny Tam 2018-11-22 19:25:30 -08:00 committed by GitHub
parent b2a195001e
commit 8e6c181c59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 121 deletions

View file

@ -1,74 +1,73 @@
<?php <?php
echo "\n"; echo "\n";
$serverName = "tcp:yourserver.database.windows.net,1433"; $serverName = "tcp:yourserver.database.windows.net,1433";
$database = "yourdatabase"; $database = "yourdatabase";
$uid = "yourusername"; $uid = "yourusername";
$pwd = "yourpassword"; $pwd = "yourpassword";
//Establishes the connection //Establishes the connection
$conn = new PDO( "sqlsrv:server=$serverName ; Database = $database", $uid, $pwd); $conn = new PDO("sqlsrv:server=$serverName ; Database = $database", $uid, $pwd);
//Select Query //Select Query
$tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; $tsql = "SELECT [CompanyName] FROM SalesLT.Customer";
//Executes the query //Executes the query
$getProducts = $conn->query( $tsql ); $getProducts = $conn->query($tsql);
//Error handling //Error handling
FormatErrors ($conn->errorInfo()); FormatErrors($conn->errorInfo());
$productCount = 0; $productCount = 0;
$ctr = 0; $ctr = 0;
?> ?>
<h1> First 10 results are : </h1> <h1> First 10 results are : </h1>
<?php <?php
while($row = $getProducts->fetch(PDO::FETCH_ASSOC)) while ($row = $getProducts->fetch(PDO::FETCH_ASSOC)) {
{ if ($ctr>9) {
if($ctr>9) break;
break; }
$ctr++; $ctr++;
echo($row['CompanyName']); echo($row['CompanyName']);
echo("<br/>"); echo("<br/>");
$productCount++; $productCount++;
} }
$getProducts = NULL; $getProducts = null;
$tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.* VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.* VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())";
//Insert query //Insert query
$insertReview = $conn->query( $tsql ); $insertReview = $conn->query($tsql);
FormatErrors ($conn->errorInfo()); FormatErrors($conn->errorInfo());
?> ?>
<h1> Product Key inserted is :</h1> <h1> Product Key inserted is :</h1>
<?php <?php
while($row = $insertReview->fetch(PDO::FETCH_ASSOC)) while ($row = $insertReview->fetch(PDO::FETCH_ASSOC)) {
{ echo($row['ProductID']."<br/>");
echo($row['ProductID']."<br/>"); }
} $insertReview = null;
$insertReview = NULL;
//Delete Query
//Delete Query //We are deleting the same record
//We are deleting the same record $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?";
$tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; $param = "SQL New 1";
$param = "SQL New 1";
$deleteReview = $conn->prepare($tsql);
$deleteReview = $conn->prepare($tsql); $deleteReview->bindParam(1, $param);
$deleteReview->bindParam(1, $param);
$deleteReview->execute();
$deleteReview->execute(); FormatErrors($deleteReview->errorInfo());
FormatErrors ($deleteReview->errorInfo());
function FormatErrors($error)
function FormatErrors( $error ) {
{ /* Display error. */
/* Display error. */ echo "Error information: <br/>";
echo "Error information: <br/>";
echo "SQLSTATE: ".$error[0]."<br/>";
echo "SQLSTATE: ".$error[0]."<br/>"; echo "Code: ".$error[1]."<br/>";
echo "Code: ".$error[1]."<br/>"; echo "Message: ".$error[2]."<br/>";
echo "Message: ".$error[2]."<br/>"; }
}
?> ?>

View file

@ -2,68 +2,70 @@
echo "\n"; echo "\n";
$serverName = "tcp:yourserver.database.windows.net,1433"; $serverName = "tcp:yourserver.database.windows.net,1433";
$connectionOptions = array("Database"=>"yourdatabase", "Uid"=>"yourusername", "PWD"=>"yourpassword"); $connectionOptions = array("Database"=>"yourdatabase", "Uid"=>"yourusername", "PWD"=>"yourpassword");
//Establishes the connection //Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions); $conn = sqlsrv_connect($serverName, $connectionOptions);
//Select Query //Select Query
$tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; $tsql = "SELECT [CompanyName] FROM SalesLT.Customer";
//Executes the query //Executes the query
$getProducts = sqlsrv_query($conn, $tsql); $getProducts = sqlsrv_query($conn, $tsql);
//Error handling //Error handling
if ($getProducts == FALSE) if ($getProducts == false) {
die(FormatErrors(sqlsrv_errors())); die(FormatErrors(sqlsrv_errors()));
$productCount = 0; }
$ctr = 0; $productCount = 0;
?> $ctr = 0;
?>
<h1> First 10 results are : </h1> <h1> First 10 results are : </h1>
<?php <?php
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
{ if ($ctr>9) {
if($ctr>9) break;
break; }
$ctr++; $ctr++;
echo($row['CompanyName']); echo($row['CompanyName']);
echo("<br/>"); echo("<br/>");
$productCount++; $productCount++;
} }
sqlsrv_free_stmt($getProducts); 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())"; $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 //Insert query
$insertReview = sqlsrv_query($conn, $tsql); $insertReview = sqlsrv_query($conn, $tsql);
if($insertReview == FALSE) if ($insertReview == false) {
die(FormatErrors( sqlsrv_errors())); die(FormatErrors(sqlsrv_errors()));
?> }
?>
<h1> Product Key inserted is :</h1> <h1> Product Key inserted is :</h1>
<?php <?php
while($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) while ($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) {
{ echo($row['ProductID']);
echo($row['ProductID']); }
} sqlsrv_free_stmt($insertReview);
sqlsrv_free_stmt($insertReview); //Delete Query
//Delete Query //We are deleting the same record
//We are deleting the same record $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?";
$tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; $params = array("SQL New 1");
$params = array("SQL New 1");
$deleteReview = sqlsrv_prepare($conn, $tsql, $params);
$deleteReview = sqlsrv_prepare($conn, $tsql, $params); if ($deleteReview == false) {
if($deleteReview == FALSE) die(FormatErrors(sqlsrv_errors()));
die(FormatErrors(sqlsrv_errors())); }
if(sqlsrv_execute($deleteReview) == FALSE) if (sqlsrv_execute($deleteReview) == false) {
die(FormatErrors(sqlsrv_errors())); die(FormatErrors(sqlsrv_errors()));
}
function FormatErrors( $errors )
{ function FormatErrors($errors)
/* Display errors. */ {
echo "Error information: <br/>"; /* Display errors. */
echo "Error information: <br/>";
foreach ( $errors as $error )
{ foreach ($errors as $error) {
echo "SQLSTATE: ".$error['SQLSTATE']."<br/>"; echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
echo "Code: ".$error['code']."<br/>"; echo "Code: ".$error['code']."<br/>";
echo "Message: ".$error['message']."<br/>"; echo "Message: ".$error['message']."<br/>";
} }
} }
?> ?>