mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Do not use double quotes where not necessary
This commit is contained in:
@@ -1,74 +1,74 @@
|
||||
<?php
|
||||
$serverName = "localhost";
|
||||
$serverName = 'localhost';
|
||||
$connectionOptions = [
|
||||
"Database" => "SampleDB",
|
||||
"Uid" => "sa",
|
||||
"PWD" => "your_password",
|
||||
'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 (?,?);";
|
||||
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 || $rowsAffected == false) {
|
||||
die(formatErrors(sqlsrv_errors()));
|
||||
}
|
||||
echo($rowsAffected . " row(s) inserted: " . PHP_EOL);
|
||||
echo($rowsAffected . ' row(s) inserted: ' . PHP_EOL);
|
||||
|
||||
sqlsrv_free_stmt($getResults);
|
||||
|
||||
// Update Query
|
||||
|
||||
$userToUpdate = 'Nikita';
|
||||
$tsql = "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?";
|
||||
$tsql = 'UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?';
|
||||
$params = ['Sweeden', $userToUpdate];
|
||||
echo("Updating Location for user " . $userToUpdate . PHP_EOL);
|
||||
echo('Updating Location for user ' . $userToUpdate . PHP_EOL);
|
||||
|
||||
$getResults = sqlsrv_query($conn, $tsql, $params);
|
||||
$rowsAffected = sqlsrv_rows_affected($getResults);
|
||||
if ($getResults == false || $rowsAffected == false) {
|
||||
die(formatErrors(sqlsrv_errors()));
|
||||
}
|
||||
echo($rowsAffected . " row(s) updated: " . PHP_EOL);
|
||||
echo($rowsAffected . ' row(s) updated: ' . PHP_EOL);
|
||||
sqlsrv_free_stmt($getResults);
|
||||
|
||||
// Delete Query
|
||||
$userToDelete = 'Jared';
|
||||
$tsql = "DELETE FROM TestSchema.Employees WHERE Name = ?";
|
||||
$tsql = 'DELETE FROM TestSchema.Employees WHERE Name = ?';
|
||||
$params = [$userToDelete];
|
||||
$getResults = sqlsrv_query($conn, $tsql, $params);
|
||||
echo("Deleting user " . $userToDelete . PHP_EOL);
|
||||
echo('Deleting user ' . $userToDelete . PHP_EOL);
|
||||
$rowsAffected = sqlsrv_rows_affected($getResults);
|
||||
if ($getResults == false || $rowsAffected == false) {
|
||||
die(formatErrors(sqlsrv_errors()));
|
||||
}
|
||||
echo($rowsAffected . " row(s) deleted: " . PHP_EOL);
|
||||
echo($rowsAffected . ' row(s) deleted: ' . PHP_EOL);
|
||||
sqlsrv_free_stmt($getResults);
|
||||
|
||||
// Read Query
|
||||
$tsql = "SELECT Id, Name, Location FROM TestSchema.Employees;";
|
||||
$tsql = 'SELECT Id, Name, Location FROM TestSchema.Employees;';
|
||||
$getResults = sqlsrv_query($conn, $tsql);
|
||||
echo("Reading data from table" . PHP_EOL);
|
||||
echo('Reading data from table' . PHP_EOL);
|
||||
if ($getResults == false) {
|
||||
die(formatErrors(sqlsrv_errors()));
|
||||
}
|
||||
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)
|
||||
{
|
||||
/* Display errors. */
|
||||
echo "Error information: ";
|
||||
echo 'Error information: ';
|
||||
|
||||
foreach ($errors as $error) {
|
||||
echo "SQLSTATE: " . $error['SQLSTATE'] . "";
|
||||
echo "Code: " . $error['code'] . "";
|
||||
echo "Message: " . $error['message'] . "";
|
||||
echo 'SQLSTATE: ' . $error['SQLSTATE'] . '';
|
||||
echo 'Code: ' . $error['code'] . '';
|
||||
echo 'Message: ' . $error['message'] . '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user