Skip to content

Commit fc94edc

Browse files
committed
Modernize and clean up cod examples
1 parent 6a4a279 commit fc94edc

10 files changed

Lines changed: 335 additions & 346 deletions

File tree

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
<?php
2-
echo "\n";
3-
$serverName = "tcp:yourserver.database.windows.net,1433";
4-
5-
$connectionOptions = array("Database"=>"yourpassword",
6-
"Uid"=>"yourusername", "PWD"=>"yourpassword");
7-
8-
$conn = sqlsrv_connect($serverName, $connectionOptions);
9-
10-
$tsql = "SELECT [CompanyName] FROM SalesLT.Customer";
11-
12-
$getProducts = sqlsrv_query($conn, $tsql);
13-
14-
if ($getProducts == FALSE)
15-
die(FormatErrors(sqlsrv_errors()));
16-
17-
$productCount = 0;
18-
$ctr = 0;
19-
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
20-
{
21-
$ctr++;
22-
echo($row['CompanyName']);
23-
echo("<br/>");
24-
$productCount++;
25-
if($ctr>10)
26-
break;
27-
}
28-
29-
sqlsrv_free_stmt($getProducts);
30-
31-
$tsql = "INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server 15', 'SQL Server 12', 0, 0, getdate())";
32-
33-
34-
$insertReview = sqlsrv_query($conn, $tsql);
35-
if($insertReview == FALSE)
36-
die(FormatErrors( sqlsrv_errors()));
37-
38-
39-
while($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC))
40-
{
41-
echo($row['ProductID']);
42-
}
43-
sqlsrv_free_stmt($insertReview);
44-
45-
$tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?";
46-
$params = array("SQL Server 15");
47-
48-
$deleteReview = sqlsrv_prepare($conn, $tsql, $params);
49-
if($deleteReview == FALSE)
50-
die(FormatErrors(sqlsrv_errors()));
51-
52-
if(sqlsrv_execute($deleteReview) == FALSE)
53-
die(FormatErrors(sqlsrv_errors()));
54-
55-
while($row = sqlsrv_fetch_array($deleteReview, SQLSRV_FETCH_ASSOC))
56-
{
57-
echo($row['ProductID']);
58-
}
59-
sqlsrv_free_stmt($deleteReview);
60-
61-
62-
?>
63-
2+
echo "\n";
3+
$serverName = "tcp:yourserver.database.windows.net,1433";
4+
5+
$connectionOptions = [
6+
"Database" => "yourpassword",
7+
"Uid" => "yourusername",
8+
"PWD" => "yourpassword",
9+
];
10+
11+
$conn = sqlsrv_connect($serverName, $connectionOptions);
12+
13+
$tsql = "SELECT [CompanyName] FROM SalesLT.Customer";
14+
15+
$getProducts = sqlsrv_query($conn, $tsql);
16+
17+
if ($getProducts == false) {
18+
die(FormatErrors(sqlsrv_errors()));
19+
}
20+
21+
$productCount = 0;
22+
$ctr = 0;
23+
while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
24+
$ctr++;
25+
echo($row['CompanyName']);
26+
echo("<br/>");
27+
$productCount++;
28+
if ($ctr > 10) {
29+
break;
30+
}
31+
}
32+
33+
sqlsrv_free_stmt($getProducts);
34+
35+
$tsql = "INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server 15', 'SQL Server 12', 0, 0, getdate())";
36+
37+
$insertReview = sqlsrv_query($conn, $tsql);
38+
if ($insertReview == false) {
39+
die(FormatErrors(sqlsrv_errors()));
40+
}
41+
42+
while ($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) {
43+
echo($row['ProductID']);
44+
}
45+
sqlsrv_free_stmt($insertReview);
46+
47+
$tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?";
48+
$params = ["SQL Server 15"];
49+
50+
$deleteReview = sqlsrv_prepare($conn, $tsql, $params);
51+
if ($deleteReview == false) {
52+
die(FormatErrors(sqlsrv_errors()));
53+
}
54+
55+
if (sqlsrv_execute($deleteReview) == false) {
56+
die(FormatErrors(sqlsrv_errors()));
57+
}
58+
59+
while ($row = sqlsrv_fetch_array($deleteReview, SQLSRV_FETCH_ASSOC)) {
60+
echo($row['ProductID']);
61+
}
62+
sqlsrv_free_stmt($deleteReview);
Lines changed: 112 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,132 @@
1-
<?php
2-
$serverName = "tcp:yourserver.database.windows.net,1433";
3-
$connectionOptions = array("Database"=>"yourdatabase",
4-
"Uid"=>"yourusername", "PWD"=>"yourpassword");
5-
//Establishes the connection
6-
$conn = sqlsrv_connect($serverName, $connectionOptions);
7-
//////////////////STORED PROCEDURE/////////////////////////
8-
$tsql = "CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END";
9-
$storedProc = sqlsrv_query($conn, $tsql);
10-
if($storedProc == FALSE){
11-
echo "Error creating Stored Procedure";
12-
die(FormatErrors( sqlsrv_errors()));
13-
}
14-
sqlsrv_free_stmt($storedProc);
1+
<?php
2+
$serverName = "tcp:yourserver.database.windows.net,1433";
3+
$connectionOptions = [
4+
"Database" => "yourdatabase",
5+
"Uid" => "yourusername",
6+
"PWD" => "yourpassword",
7+
];
8+
//Establishes the connection
9+
$conn = sqlsrv_connect($serverName, $connectionOptions);
10+
//////////////////STORED PROCEDURE/////////////////////////
11+
$tsql = "CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END";
12+
$storedProc = sqlsrv_query($conn, $tsql);
13+
if ($storedProc == false) {
14+
echo "Error creating Stored Procedure";
15+
die(FormatErrors(sqlsrv_errors()));
16+
}
17+
sqlsrv_free_stmt($storedProc);
1518

16-
$tsql = "exec sp_GETCompanies22";
17-
//Executes the query
18-
$getProducts = sqlsrv_query($conn, $tsql);
19-
//Error handling
20-
if ($getProducts == FALSE){
21-
echo "Error executing Stored Procedure";
22-
die(FormatErrors(sqlsrv_errors()));
23-
}
24-
$productCount = 0;
25-
$ctr = 0;
26-
?>
27-
<h1> First 10 results are after executing the stored procedure: </h1>
19+
$tsql = "exec sp_GETCompanies22";
20+
//Executes the query
21+
$getProducts = sqlsrv_query($conn, $tsql);
22+
//Error handling
23+
if ($getProducts == false) {
24+
echo "Error executing Stored Procedure";
25+
die(FormatErrors(sqlsrv_errors()));
26+
}
27+
$productCount = 0;
28+
$ctr = 0;
29+
?>
30+
<h1> First 10 results are after executing the stored procedure: </h1>
2831
<?php
29-
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)){
30-
//Printing only the first 10 results
31-
if($ctr>9)
32-
break;
33-
$ctr++;
34-
echo($row['CompanyName']);
35-
echo("<br/>");
36-
$productCount++;
32+
while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
33+
//Printing only the first 10 results
34+
if ($ctr > 9) {
35+
break;
3736
}
38-
sqlsrv_free_stmt($getProducts);
39-
$tsql = "DROP PROCEDURE sp_GETCompanies22";
37+
$ctr++;
38+
echo($row['CompanyName']);
39+
echo("<br/>");
40+
$productCount++;
41+
}
42+
sqlsrv_free_stmt($getProducts);
43+
$tsql = "DROP PROCEDURE sp_GETCompanies22";
4044

41-
$storedProc = sqlsrv_query($conn, $tsql);
42-
if($storedProc == FALSE)
43-
{
44-
echo "Error dropping Stored Procedure";
45-
die(FormatErrors( sqlsrv_errors()));
46-
}
47-
sqlsrv_free_stmt($storedProc);
45+
$storedProc = sqlsrv_query($conn, $tsql);
46+
if ($storedProc == false) {
47+
echo "Error dropping Stored Procedure";
48+
die(FormatErrors(sqlsrv_errors()));
49+
}
50+
sqlsrv_free_stmt($storedProc);
4851
?>
4952
<?php
50-
//////////////////TRANSACTION/////////////////////////
51-
if (sqlsrv_begin_transaction($conn) == FALSE)
52-
{
53-
echo "Error opening connection";
54-
die(FormatErrors(sqlsrv_errors()));
55-
}
53+
//////////////////TRANSACTION/////////////////////////
54+
if (sqlsrv_begin_transaction($conn) == false) {
55+
echo "Error opening connection";
56+
die(FormatErrors(sqlsrv_errors()));
57+
}
5658

57-
/* Set up and execute the first query. */
58-
$tsql1 = "INSERT INTO SalesLT.SalesOrderDetail
59+
/* Set up and execute the first query. */
60+
$tsql1 = "INSERT INTO SalesLT.SalesOrderDetail
5961
(SalesOrderID,OrderQty,ProductID,UnitPrice)
6062
VALUES (71774, 22, 709, 33)";
61-
$stmt1 = sqlsrv_query($conn, $tsql1);
63+
$stmt1 = sqlsrv_query($conn, $tsql1);
6264

63-
/* Set up and execute the second query. */
64-
$tsql2 = "UPDATE SalesLT.SalesOrderDetail SET OrderQty = (OrderQty + 1) WHERE ProductID = 709";
65-
$stmt2 = sqlsrv_query( $conn, $tsql2);
65+
/* Set up and execute the second query. */
66+
$tsql2 = "UPDATE SalesLT.SalesOrderDetail SET OrderQty = (OrderQty + 1) WHERE ProductID = 709";
67+
$stmt2 = sqlsrv_query($conn, $tsql2);
6668

67-
/* If both queries were successful, commit the transaction. */
68-
/* Otherwise, rollback the transaction. */
69-
if($stmt1 && $stmt2)
70-
{
71-
sqlsrv_commit($conn);
72-
?>
73-
<h1> Transaction was commited </h1>
69+
/* If both queries were successful, commit the transaction. */
70+
/* Otherwise, rollback the transaction. */
71+
if ($stmt1 && $stmt2) {
72+
sqlsrv_commit($conn);
73+
?>
74+
<h1> Transaction was commited </h1>
7475

75-
<?php
76-
77-
}
78-
else
79-
{
80-
sqlsrv_rollback($conn);
81-
echo "Transaction was rolled back.\n";
82-
}
76+
<?php
77+
78+
} else {
79+
sqlsrv_rollback($conn);
80+
echo "Transaction was rolled back.\n";
81+
}
8382

84-
/* Free statement and connection resources. */
85-
sqlsrv_free_stmt( $stmt1);
86-
sqlsrv_free_stmt( $stmt2);
83+
/* Free statement and connection resources. */
84+
sqlsrv_free_stmt($stmt1);
85+
sqlsrv_free_stmt($stmt2);
8786

8887
?>
8988
<?php
90-
//////////////////UDF/////////////////////////
91-
//Dropping function if it already exists
92-
$tsql1 = "IF OBJECT_ID(N'dbo.ifGetTotalItems', N'IF') IS NOT NULL DROP FUNCTION dbo.ifGetTotalItems;";
93-
$getProducts = sqlsrv_query($conn, $tsql1);
94-
//Error handling
95-
if ($getProducts == FALSE)
96-
{
97-
echo "Error deleting the UDF";
98-
die(FormatErrors(sqlsrv_errors()));
99-
}
100-
$tsql1 = "CREATE FUNCTION dbo.ifGetTotalItems (@OrderID INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN (SELECT SUM(OrderQty) AS TotalItems FROM SalesLT.SalesOrderDetail WHERE SalesOrderID = @OrderID GROUP BY SalesOrderID);";
101-
$getProducts = sqlsrv_query($conn, $tsql1);
102-
//Error handling
103-
if ($getProducts == FALSE)
104-
{
105-
echo "Error creating the UDF";
106-
die(FormatErrors(sqlsrv_errors()));
107-
}
108-
$tsql1 = "SELECT s.SalesOrderID, s.OrderDate, s.CustomerID, f.TotalItems FROM SalesLT.SalesOrderHeader s CROSS APPLY dbo.ifGetTotalItems(s.SalesOrderID) f ORDER BY SalesOrderID;";
109-
$getProducts = sqlsrv_query($conn, $tsql1);
110-
//Error handling
111-
if ($getProducts == FALSE)
112-
{
113-
echo "Error executing the UDF";
114-
die(FormatErrors(sqlsrv_errors()));
115-
}
116-
$productCount = 0;
117-
$ctr = 0;
118-
?>
119-
<h1> First 10 results are after executing a query that uses the UDF: </h1>
89+
//////////////////UDF/////////////////////////
90+
//Dropping function if it already exists
91+
$tsql1 = "IF OBJECT_ID(N'dbo.ifGetTotalItems', N'IF') IS NOT NULL DROP FUNCTION dbo.ifGetTotalItems;";
92+
$getProducts = sqlsrv_query($conn, $tsql1);
93+
//Error handling
94+
if ($getProducts == false) {
95+
echo "Error deleting the UDF";
96+
die(FormatErrors(sqlsrv_errors()));
97+
}
98+
$tsql1 = "CREATE FUNCTION dbo.ifGetTotalItems (@OrderID INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN (SELECT SUM(OrderQty) AS TotalItems FROM SalesLT.SalesOrderDetail WHERE SalesOrderID = @OrderID GROUP BY SalesOrderID);";
99+
$getProducts = sqlsrv_query($conn, $tsql1);
100+
//Error handling
101+
if ($getProducts == false) {
102+
echo "Error creating the UDF";
103+
die(FormatErrors(sqlsrv_errors()));
104+
}
105+
$tsql1 = "SELECT s.SalesOrderID, s.OrderDate, s.CustomerID, f.TotalItems FROM SalesLT.SalesOrderHeader s CROSS APPLY dbo.ifGetTotalItems(s.SalesOrderID) f ORDER BY SalesOrderID;";
106+
$getProducts = sqlsrv_query($conn, $tsql1);
107+
//Error handling
108+
if ($getProducts == false) {
109+
echo "Error executing the UDF";
110+
die(FormatErrors(sqlsrv_errors()));
111+
}
112+
$productCount = 0;
113+
$ctr = 0;
114+
?>
115+
<h1> First 10 results are after executing a query that uses the UDF: </h1>
120116
<?php
121-
echo "SalesOrderID CustomerID TotalItems";
122-
echo("<br/>");
117+
echo "SalesOrderID CustomerID TotalItems";
118+
echo("<br/>");
123119

124-
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
125-
{
126-
//Printing only the top 10 results
127-
if($ctr>9)
128-
break;
129-
$ctr++;
130-
echo $row['SalesOrderID'] . str_repeat('&nbsp;', 13) . $row['CustomerID'] . str_repeat('&nbsp;', 11) . $row['TotalItems'];
131-
echo("<br/>");
132-
$productCount++;
133-
120+
while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) {
121+
//Printing only the top 10 results
122+
if ($ctr > 9) {
123+
break;
134124
}
135-
sqlsrv_free_stmt($getProducts);
136-
125+
$ctr++;
126+
echo $row['SalesOrderID'] . str_repeat('&nbsp;', 13) . $row['CustomerID'] . str_repeat('&nbsp;',
127+
11) . $row['TotalItems'];
128+
echo("<br/>");
129+
$productCount++;
137130

138-
?>
131+
}
132+
sqlsrv_free_stmt($getProducts);

0 commit comments

Comments
 (0)