diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..520d2a04af --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*.php] +indent_style = space +indent_size = 4 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.bat] +end_of_line = crlf diff --git a/.gitignore b/.gitignore index 6735850e2b..1ace7a7039 100644 --- a/.gitignore +++ b/.gitignore @@ -441,3 +441,6 @@ samples/features/sql-management-objects/src/out/CodeCoverage/CodeCoverage.config # Certificates *.pem *.p12 + +# Composer +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000000..f5c8f0b3d5 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "microsoft/sql-server-samples", + "description": "Official Microsoft GitHub Repository containing code samples for SQL Server", + "type": "project", + "require": { + "php": ">=7.0.0" + }, + "require-dev": { + "squizlabs/php_codesniffer": "^3.5" + }, + "license": "MIT", + "minimum-stability": "stable", + "scripts": { + "cs-check": "phpcs -p --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1", + "cs-fix": "phpcbf -p --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000000..2a25733c91 --- /dev/null +++ b/composer.lock @@ -0,0 +1,72 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "49a34ccb792321ec81984328df884636", + "packages": [], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "3.5.5", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2020-04-17T01:09:41+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=7.0.0" + }, + "platform-dev": [], + "plugin-api-version": "1.1.0" +} diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000000..b7ca31864a --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,52 @@ + + + The coding standard for MS SQL Server PHP Examples. + + + samples/tutorials/php + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/tutorials/php/1.0 PHP Configuration and Getting Started/test.php b/samples/tutorials/php/1.0 PHP Configuration and Getting Started/test.php index 1ad8faa394..568aa0b53d 100644 --- a/samples/tutorials/php/1.0 PHP Configuration and Getting Started/test.php +++ b/samples/tutorials/php/1.0 PHP Configuration and Getting Started/test.php @@ -1,63 +1,68 @@ "yourpassword", - "Uid"=>"yourusername", "PWD"=>"yourpassword"); - - $conn = sqlsrv_connect($serverName, $connectionOptions); - - $tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; - - $getProducts = sqlsrv_query($conn, $tsql); - - if ($getProducts == FALSE) - die(FormatErrors(sqlsrv_errors())); - - $productCount = 0; - $ctr = 0; - while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) - { - $ctr++; - echo($row['CompanyName']); - echo("
"); - $productCount++; - if($ctr>10) - break; - } - - sqlsrv_free_stmt($getProducts); - - $tsql = "INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server 15', 'SQL Server 12', 0, 0, getdate())"; - - - $insertReview = sqlsrv_query($conn, $tsql); - if($insertReview == FALSE) - die(FormatErrors( sqlsrv_errors())); - - - while($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) - { - echo($row['ProductID']); - } - sqlsrv_free_stmt($insertReview); - - $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; - $params = array("SQL Server 15"); - - $deleteReview = sqlsrv_prepare($conn, $tsql, $params); - if($deleteReview == FALSE) - die(FormatErrors(sqlsrv_errors())); - - if(sqlsrv_execute($deleteReview) == FALSE) - die(FormatErrors(sqlsrv_errors())); - - while($row = sqlsrv_fetch_array($deleteReview, SQLSRV_FETCH_ASSOC)) - { - echo($row['ProductID']); - } - sqlsrv_free_stmt($deleteReview); - - -?> - +echo "\n"; +$serverName = 'tcp:your_server.database.windows.net,1433'; + +$connectionOptions = [ + 'Database' => 'your_database', + 'Uid' => 'your_username', + 'PWD' => 'your_password', +]; + +$conn = sqlsrv_connect($serverName, $connectionOptions); + +$tsql = 'SELECT [CompanyName] FROM SalesLT.Customer'; + +$getProducts = sqlsrv_query($conn, $tsql); + +if ($getProducts === false) { + format_errors(sqlsrv_errors()); + die(); +} + +$productCount = 0; +$ctr = 0; +while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) { + $ctr++; + echo($row['CompanyName']); + echo('
'); + $productCount++; + if ($ctr > 10) { + break; + } +} + +sqlsrv_free_stmt($getProducts); + +$tsql = "INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) +OUTPUT INSERTED.ProductID +VALUES ('SQL Server 15', 'SQL Server 12', 0, 0, getdate());"; + +$insertReview = sqlsrv_query($conn, $tsql); +if ($insertReview === false) { + format_errors(sqlsrv_errors()); + die(); +} + +while ($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC)) { + echo($row['ProductID']); +} +sqlsrv_free_stmt($insertReview); + +$tsql = 'DELETE FROM [SalesLT].[Product] WHERE Name=?'; +$params = ['SQL Server 15']; + +$deleteReview = sqlsrv_prepare($conn, $tsql, $params); +if ($deleteReview === false) { + format_errors(sqlsrv_errors()); + die(); +} + +if (sqlsrv_execute($deleteReview) === false) { + format_errors(sqlsrv_errors()); + die(); +} + +while ($row = sqlsrv_fetch_array($deleteReview, SQLSRV_FETCH_ASSOC)) { + echo($row['ProductID']); +} +sqlsrv_free_stmt($deleteReview); diff --git a/samples/tutorials/php/2.0 PHP Server programming - Stored procedures, Transactions, and UDFs/test.php b/samples/tutorials/php/2.0 PHP Server programming - Stored procedures, Transactions, and UDFs/test.php index 06bd878591..f9175ff519 100644 --- a/samples/tutorials/php/2.0 PHP Server programming - Stored procedures, Transactions, and UDFs/test.php +++ b/samples/tutorials/php/2.0 PHP Server programming - Stored procedures, Transactions, and UDFs/test.php @@ -1,138 +1,159 @@ -"yourdatabase", - "Uid"=>"yourusername", "PWD"=>"yourpassword"); - //Establishes the connection - $conn = sqlsrv_connect($serverName, $connectionOptions); - //////////////////STORED PROCEDURE///////////////////////// - $tsql = "CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END"; - $storedProc = sqlsrv_query($conn, $tsql); - if($storedProc == FALSE){ - echo "Error creating Stored Procedure"; - die(FormatErrors( sqlsrv_errors())); - } - sqlsrv_free_stmt($storedProc); + 'your_database', + 'Uid' => 'your_username', + 'PWD' => ' your_password', +]; +// Establishes the connection +$conn = sqlsrv_connect($serverName, $connectionOptions); + +/* + * Stored Procedure + */ + +$tsql = 'CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END'; +$storedProc = sqlsrv_query($conn, $tsql); +if ($storedProc === false) { + echo 'Error creating Stored Procedure'; + format_errors(sqlsrv_errors()); + die(); +} +sqlsrv_free_stmt($storedProc); - $tsql = "exec sp_GETCompanies22"; - //Executes the query - $getProducts = sqlsrv_query($conn, $tsql); - //Error handling - if ($getProducts == FALSE){ - echo "Error executing Stored Procedure"; - die(FormatErrors(sqlsrv_errors())); - } - $productCount = 0; - $ctr = 0; -?> -

First 10 results are after executing the stored procedure:

+$tsql = 'exec sp_GETCompanies22'; +// Executes the query +$getProducts = sqlsrv_query($conn, $tsql); +// Error handling +if ($getProducts === false) { + echo 'Error executing Stored Procedure'; + format_errors(sqlsrv_errors()); + die(); +} +$productCount = 0; +$ctr = 0; +?> +

First 10 results are after executing the stored procedure:

9) - break; - $ctr++; - echo($row['CompanyName']); - echo("
"); - $productCount++; +while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) { + // Printing only the first 10 results + if ($ctr > 9) { + break; } - sqlsrv_free_stmt($getProducts); - $tsql = "DROP PROCEDURE sp_GETCompanies22"; + $ctr++; + echo($row['CompanyName']); + echo('
'); + $productCount++; +} +sqlsrv_free_stmt($getProducts); +$tsql = 'DROP PROCEDURE sp_GETCompanies22'; - $storedProc = sqlsrv_query($conn, $tsql); - if($storedProc == FALSE) - { - echo "Error dropping Stored Procedure"; - die(FormatErrors( sqlsrv_errors())); - } - sqlsrv_free_stmt($storedProc); +$storedProc = sqlsrv_query($conn, $tsql); +if ($storedProc === false) { + echo 'Error dropping Stored Procedure'; + format_errors(sqlsrv_errors()); + die(); +} +sqlsrv_free_stmt($storedProc); ?> -

Transaction was commited

+/* If both queries were successful, commit the transaction. */ +/* Otherwise, rollback the transaction. */ +if ($stmt1 && $stmt2) { + sqlsrv_commit($conn); + ?> +

Transaction was committed

- -

First 10 results are after executing a query that uses the UDF:

+/* + * UDF + */ +// Dropping function if it already exists +$tsql1 = "IF OBJECT_ID(N'dbo.ifGetTotalItems', N'IF') IS NOT NULL DROP FUNCTION dbo.ifGetTotalItems;"; +$getProducts = sqlsrv_query($conn, $tsql1); +// Error handling +if ($getProducts === false) { + echo 'Error deleting the UDF'; + format_errors(sqlsrv_errors()); + die(); +} +$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 +);'; +$getProducts = sqlsrv_query($conn, $tsql1); +// Error handling +if ($getProducts === false) { + echo 'Error creating the UDF'; + format_errors(sqlsrv_errors()); + die(); +} +$tsql1 = 'SELECT s.SalesOrderID, s.OrderDate, s.CustomerID, f.TotalItems +FROM SalesLT.SalesOrderHeader s +CROSS APPLY dbo.ifGetTotalItems(s.SalesOrderID) f +ORDER BY SalesOrderID;'; +$getProducts = sqlsrv_query($conn, $tsql1); +// Error handling +if ($getProducts === false) { + echo 'Error executing the UDF'; + format_errors(sqlsrv_errors()); + die(); +} +$productCount = 0; +$ctr = 0; +?> +

First 10 results are after executing a query that uses the UDF:

"); +echo 'SalesOrderID CustomerID TotalItems'; +echo('
'); - while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) - { - //Printing only the top 10 results - if($ctr>9) - break; - $ctr++; - echo $row['SalesOrderID'] . str_repeat(' ', 13) . $row['CustomerID'] . str_repeat(' ', 11) . $row['TotalItems']; - echo("
"); - $productCount++; - +while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) { + // Printing only the top 10 results + if ($ctr > 9) { + break; } - sqlsrv_free_stmt($getProducts); - - -?> \ No newline at end of file + $ctr++; + echo sprintf( + '%s%s%s%s%s', + $row['SalesOrderID'], + str_repeat(' ', 13), + $row['CustomerID'], + str_repeat(' ', 11), + $row['TotalItems'] + ); + echo('
'); + $productCount++; +} +sqlsrv_free_stmt($getProducts); diff --git a/samples/tutorials/php/RHEL/SqlServerColumnstoreSample/columnstore.php b/samples/tutorials/php/RHEL/SqlServerColumnstoreSample/columnstore.php index 12aaa0b264..cc9a0f1adf 100644 --- a/samples/tutorials/php/RHEL/SqlServerColumnstoreSample/columnstore.php +++ b/samples/tutorials/php/RHEL/SqlServerColumnstoreSample/columnstore.php @@ -1,42 +1,40 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Read Query -$tsql= "SELECT SUM(Price) as sum FROM Table_with_5M_rows"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Sum: "); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT SUM(Price) as sum FROM Table_with_5M_rows'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Sum: '); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['sum'] . PHP_EOL); - + echo($row['sum'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -$time_end = microtime(true); -$execution_time = round((($time_end - $time_start)*1000),2); -echo 'QueryTime: '.$execution_time.' ms'; - -?> \ No newline at end of file +$timeEnd = microtime(true); +$executionTime = round((($timeEnd - $timeStart) * 1000), 2); +echo 'QueryTime: ' . $executionTime . ' ms'; diff --git a/samples/tutorials/php/RHEL/SqlServerSample/connect.php b/samples/tutorials/php/RHEL/SqlServerSample/connect.php index 506ae39d03..905f5a0c6d 100644 --- a/samples/tutorials/php/RHEL/SqlServerSample/connect.php +++ b/samples/tutorials/php/RHEL/SqlServerSample/connect.php @@ -1,12 +1,12 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -if($conn) - echo "Connected!" -?> \ No newline at end of file +if ($conn) { + echo 'Connected!'; +} diff --git a/samples/tutorials/php/RHEL/SqlServerSample/crud.php b/samples/tutorials/php/RHEL/SqlServerSample/crud.php index 55c0552749..d0a1583b85 100644 --- a/samples/tutorials/php/RHEL/SqlServerSample/crud.php +++ b/samples/tutorials/php/RHEL/SqlServerSample/crud.php @@ -1,74 +1,78 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Insert Query -echo ("Inserting a new row into table" . PHP_EOL); -$tsql= "INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);"; -$params = array('Jake','United States'); -$getResults= sqlsrv_query($conn, $tsql, $params); +// Insert Query +echo('Inserting a new row into table' . PHP_EOL); +$tsql = 'INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);'; +$params = ['Jake', 'United States']; +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) inserted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) inserted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Update Query +// Update Query $userToUpdate = 'Nikita'; -$tsql= "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?"; -$params = array('Sweeden', $userToUpdate); -echo("Updating Location for user " . $userToUpdate . PHP_EOL); +$tsql = 'UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?'; +$params = ['Sweden', $userToUpdate]; +echo('Updating Location for user ' . $userToUpdate . PHP_EOL); -$getResults= sqlsrv_query($conn, $tsql, $params); +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) updated: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) updated: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Delte Query +// Delete Query $userToDelete = 'Jared'; -$tsql= "DELETE FROM TestSchema.Employees WHERE Name = ?"; -$params = array($userToDelete); -$getResults= sqlsrv_query($conn, $tsql, $params); -echo("Deleting user " . $userToDelete . PHP_EOL); +$tsql = 'DELETE FROM TestSchema.Employees WHERE Name = ?'; +$params = [$userToDelete]; +$getResults = sqlsrv_query($conn, $tsql, $params); +echo('Deleting user ' . $userToDelete . PHP_EOL); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) deleted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) deleted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); - -//Read Query -$tsql= "SELECT Id, Name, Location FROM TestSchema.Employees;"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Reading data from table" . PHP_EOL); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT Id, Name, Location FROM TestSchema.Employees;'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Reading data from table' . PHP_EOL); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['Id'] . " " . $row['Name'] . " " . $row['Location'] . PHP_EOL); - + echo($row['Id'] . ' ' . $row['Name'] . ' ' . $row['Location'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -?> \ No newline at end of file diff --git a/samples/tutorials/php/Ubuntu/SqlServerColumnstoreSample/columnstore.php b/samples/tutorials/php/Ubuntu/SqlServerColumnstoreSample/columnstore.php index 12aaa0b264..cc9a0f1adf 100644 --- a/samples/tutorials/php/Ubuntu/SqlServerColumnstoreSample/columnstore.php +++ b/samples/tutorials/php/Ubuntu/SqlServerColumnstoreSample/columnstore.php @@ -1,42 +1,40 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Read Query -$tsql= "SELECT SUM(Price) as sum FROM Table_with_5M_rows"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Sum: "); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT SUM(Price) as sum FROM Table_with_5M_rows'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Sum: '); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['sum'] . PHP_EOL); - + echo($row['sum'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -$time_end = microtime(true); -$execution_time = round((($time_end - $time_start)*1000),2); -echo 'QueryTime: '.$execution_time.' ms'; - -?> \ No newline at end of file +$timeEnd = microtime(true); +$executionTime = round((($timeEnd - $timeStart) * 1000), 2); +echo 'QueryTime: ' . $executionTime . ' ms'; diff --git a/samples/tutorials/php/Ubuntu/SqlServerSample/connect.php b/samples/tutorials/php/Ubuntu/SqlServerSample/connect.php index cdf977ec6b..f7aff3ab99 100644 --- a/samples/tutorials/php/Ubuntu/SqlServerSample/connect.php +++ b/samples/tutorials/php/Ubuntu/SqlServerSample/connect.php @@ -1,14 +1,14 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password' ]; // Establish the connection $connection = sqlsrv_connect($serverName, $connectionOptions); if ($connection) { - echo "Connected!"; + echo 'Connected!'; } diff --git a/samples/tutorials/php/Ubuntu/SqlServerSample/crud.php b/samples/tutorials/php/Ubuntu/SqlServerSample/crud.php index 55c0552749..977f214bea 100644 --- a/samples/tutorials/php/Ubuntu/SqlServerSample/crud.php +++ b/samples/tutorials/php/Ubuntu/SqlServerSample/crud.php @@ -1,74 +1,78 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Insert Query -echo ("Inserting a new row into table" . PHP_EOL); -$tsql= "INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);"; -$params = array('Jake','United States'); -$getResults= sqlsrv_query($conn, $tsql, $params); +// Insert Query +echo('Inserting a new row into table' . PHP_EOL); +$tsql = 'INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);'; +$params = ['Jake', 'United States']; +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) inserted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) inserted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Update Query +// Update Query $userToUpdate = 'Nikita'; -$tsql= "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?"; -$params = array('Sweeden', $userToUpdate); -echo("Updating Location for user " . $userToUpdate . PHP_EOL); +$tsql = 'UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?'; +$params = ['Sweden', $userToUpdate]; +echo('Updating Location for user ' . $userToUpdate . PHP_EOL); -$getResults= sqlsrv_query($conn, $tsql, $params); +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) updated: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) updated: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Delte Query +// Delete Query $userToDelete = 'Jared'; -$tsql= "DELETE FROM TestSchema.Employees WHERE Name = ?"; -$params = array($userToDelete); -$getResults= sqlsrv_query($conn, $tsql, $params); -echo("Deleting user " . $userToDelete . PHP_EOL); +$tsql = 'DELETE FROM TestSchema.Employees WHERE Name = ?'; +$params = [$userToDelete]; +$getResults = sqlsrv_query($conn, $tsql, $params); +echo('Deleting user ' . $userToDelete . PHP_EOL); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) deleted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) deleted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); - -//Read Query -$tsql= "SELECT Id, Name, Location FROM TestSchema.Employees;"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Reading data from table" . PHP_EOL); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT Id, Name, Location FROM TestSchema.Employees;'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Reading data from table' . PHP_EOL); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['Id'] . " " . $row['Name'] . " " . $row['Location'] . PHP_EOL); - + echo($row['Id'] . ' ' . $row['Name'] . ' ' . $row['Location'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -?> \ No newline at end of file diff --git a/samples/tutorials/php/Windows/SqlServerColumnstoreSample/columnstore.php b/samples/tutorials/php/Windows/SqlServerColumnstoreSample/columnstore.php index 12aaa0b264..cc9a0f1adf 100644 --- a/samples/tutorials/php/Windows/SqlServerColumnstoreSample/columnstore.php +++ b/samples/tutorials/php/Windows/SqlServerColumnstoreSample/columnstore.php @@ -1,42 +1,40 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Read Query -$tsql= "SELECT SUM(Price) as sum FROM Table_with_5M_rows"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Sum: "); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT SUM(Price) as sum FROM Table_with_5M_rows'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Sum: '); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['sum'] . PHP_EOL); - + echo($row['sum'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -$time_end = microtime(true); -$execution_time = round((($time_end - $time_start)*1000),2); -echo 'QueryTime: '.$execution_time.' ms'; - -?> \ No newline at end of file +$timeEnd = microtime(true); +$executionTime = round((($timeEnd - $timeStart) * 1000), 2); +echo 'QueryTime: ' . $executionTime . ' ms'; diff --git a/samples/tutorials/php/Windows/SqlServerSample/connect.php b/samples/tutorials/php/Windows/SqlServerSample/connect.php index 506ae39d03..905f5a0c6d 100644 --- a/samples/tutorials/php/Windows/SqlServerSample/connect.php +++ b/samples/tutorials/php/Windows/SqlServerSample/connect.php @@ -1,12 +1,12 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -if($conn) - echo "Connected!" -?> \ No newline at end of file +if ($conn) { + echo 'Connected!'; +} diff --git a/samples/tutorials/php/Windows/SqlServerSample/crud.php b/samples/tutorials/php/Windows/SqlServerSample/crud.php index 4e819f6a08..977f214bea 100644 --- a/samples/tutorials/php/Windows/SqlServerSample/crud.php +++ b/samples/tutorials/php/Windows/SqlServerSample/crud.php @@ -1,74 +1,78 @@ "SampleDB", - "Uid" => "sa", - "PWD" => "your_password" -); -//Establishes the connection +$serverName = 'localhost'; +$connectionOptions = [ + 'Database' => 'SampleDB', + 'Uid' => 'sa', + 'PWD' => 'your_password', +]; +// Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); -//Insert Query -echo ("Inserting a new row into table" . PHP_EOL); -$tsql= "INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);"; -$params = array('Jake','United States'); -$getResults= sqlsrv_query($conn, $tsql, $params); +// Insert Query +echo('Inserting a new row into table' . PHP_EOL); +$tsql = 'INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);'; +$params = ['Jake', 'United States']; +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) inserted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) inserted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Update Query +// Update Query $userToUpdate = 'Nikita'; -$tsql= "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?"; -$params = array('Sweeden', $userToUpdate); -echo("Updating Location for user " . $userToUpdate . PHP_EOL); +$tsql = 'UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?'; +$params = ['Sweden', $userToUpdate]; +echo('Updating Location for user ' . $userToUpdate . PHP_EOL); -$getResults= sqlsrv_query($conn, $tsql, $params); +$getResults = sqlsrv_query($conn, $tsql, $params); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) updated: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) updated: ' . PHP_EOL); sqlsrv_free_stmt($getResults); -//Delete Query +// Delete Query $userToDelete = 'Jared'; -$tsql= "DELETE FROM TestSchema.Employees WHERE Name = ?"; -$params = array($userToDelete); -$getResults= sqlsrv_query($conn, $tsql, $params); -echo("Deleting user " . $userToDelete . PHP_EOL); +$tsql = 'DELETE FROM TestSchema.Employees WHERE Name = ?'; +$params = [$userToDelete]; +$getResults = sqlsrv_query($conn, $tsql, $params); +echo('Deleting user ' . $userToDelete . PHP_EOL); $rowsAffected = sqlsrv_rows_affected($getResults); -if ($getResults == FALSE or $rowsAffected == FALSE) - die(FormatErrors(sqlsrv_errors())); -echo ($rowsAffected. " row(s) deleted: " . PHP_EOL); +if ($getResults === false || $rowsAffected === false) { + format_errors(sqlsrv_errors()); + die(); +} +echo($rowsAffected . ' row(s) deleted: ' . PHP_EOL); sqlsrv_free_stmt($getResults); - -//Read Query -$tsql= "SELECT Id, Name, Location FROM TestSchema.Employees;"; -$getResults= sqlsrv_query($conn, $tsql); -echo ("Reading data from table" . PHP_EOL); -if ($getResults == FALSE) - die(FormatErrors(sqlsrv_errors())); +// Read Query +$tsql = 'SELECT Id, Name, Location FROM TestSchema.Employees;'; +$getResults = sqlsrv_query($conn, $tsql); +echo('Reading data from table' . PHP_EOL); +if ($getResults === false) { + format_errors(sqlsrv_errors()); + die(); +} while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { - echo ($row['Id'] . " " . $row['Name'] . " " . $row['Location'] . PHP_EOL); - + echo($row['Id'] . ' ' . $row['Name'] . ' ' . $row['Location'] . PHP_EOL); } sqlsrv_free_stmt($getResults); -function FormatErrors( $errors ) +function format_errors($errors) { /* Display errors. */ - echo "Error information: "; + echo 'Error information: '; - foreach ( $errors as $error ) - { - echo "SQLSTATE: ".$error['SQLSTATE'].""; - echo "Code: ".$error['code'].""; - echo "Message: ".$error['message'].""; + foreach ($errors as $error) { + echo 'SQLSTATE: ' . $error['SQLSTATE'] . ''; + echo 'Code: ' . $error['code'] . ''; + echo 'Message: ' . $error['message'] . ''; } } -?>