Merge pull request #420 from yitam/newTests

Upload autonomous tests + BVT tests
This commit is contained in:
Jenny Tam 2017-06-05 14:56:12 -07:00 committed by GitHub
commit 45d87b7a96
114 changed files with 7076 additions and 0 deletions

View file

@ -0,0 +1,60 @@
<?php
function RestartConn($server)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$restart_string = "$powershell (get-service -ComputerName $server -Name mssqlserver).Stop()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
// Wait until the service is fully stopped
while (substr_count($servstring, "Stopped") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
}
$restart_string = "$powershell (get-service -ComputerName $server -Name mssqlserver).Start()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
// Wait until the service is fully started
while (substr_count($servstring, "Running") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
}
}
function StopConn($server)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$restart_string = "$powershell (get-service -ComputerName $server -Name mssqlserver).Stop()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
// Wait until the service is fully stopped
while (substr_count($servstring, "Stopped") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
}
}
function StartConn($server)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
if (substr_count($servstring, "Running") != 1)
{
$restart_string = "$powershell (get-service -ComputerName $server -Name mssqlserver).Start()";
exec( $restart_string );
}
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
// Wait until the service is fully started
while (substr_count($servstring, "Running") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $server -Name mssqlserver");
}
}
?>

View file

@ -0,0 +1,82 @@
<?php
require_once("ConInfo.inc");
// Using the tempdb database for two tables specifically constructed
// for the connection resiliency tests
$dbName = "tempdb";
$tableName1 = "test_connres1";
$tableName2 = "test_connres2";
// Generate tables for use with the connection resiliency tests.
// Using generated tables will eventually allow us to put the
// connection resiliency tests on Github, since the integrated testing
// from AppVeyor does not have AdventureWorks.
function GenerateTables( $server, $uid, $pwd, $dbName, $tableName1, $tableName2 )
{
$conn = new PDO( "sqlsrv:server = $server ; Database = $dbName ;", $uid, $pwd );
if ( $conn === false )
{
die ( print_r( sqlsrv_errors() ) );
}
// Create table
$sql = "CREATE TABLE $tableName1 ( c1 INT, c2 VARCHAR(40) )";
$stmt = $conn->query( $sql );
// Insert data
$sql = "INSERT INTO $tableName1 VALUES ( ?, ? )";
for( $t = 100; $t < 116; $t++ )
{
$stmt = $conn->prepare( $sql );
$ts = substr( sha1( $t ),0,5 );
$params = array( $t,$ts );
$stmt->execute( $params );
}
// Create table
$sql = "CREATE TABLE $tableName2 ( c1 INT, c2 VARCHAR(40) )";
$stmt = $conn->query( $sql );
// Insert data
$sql = "INSERT INTO $tableName2 VALUES ( ?, ? )";
for( $t = 200; $t < 209; $t++ )
{
$stmt = $conn->prepare( $sql );
$ts = substr( sha1( $t ),0,5 );
$params = array( $t,$ts );
$stmt->execute( $params );
}
$conn = null;
}
// Break connection by getting the session ID and killing it.
// Note that breaking a connection and testing reconnection requires a
// TCP/IP protocol connection (as opposed to a Shared Memory protocol).
function BreakConnection( $conn, $conn_break )
{
$stmt1 = $conn->query( "SELECT @@SPID" );
$obj = $stmt1->fetch( PDO::FETCH_NUM );
$spid = $obj[0];
$stmt2 = $conn_break->query( "KILL ".$spid );
sleep(1);
}
// Remove any databases previously created by GenerateDatabase
function DropTables( $server, $uid, $pwd, $tableName1, $tableName2 )
{
$conn = new PDO( "sqlsrv:server = $server ; ", $uid, $pwd );
$query="IF OBJECT_ID('tempdb.dbo.$tableName1', 'U') IS NOT NULL DROP TABLE tempdb.dbo.$tableName1";
$stmt=$conn->query( $query );
$query="IF OBJECT_ID('tempdb.dbo.$tableName2', 'U') IS NOT NULL DROP TABLE tempdb.dbo.$tableName2";
$stmt=$conn->query( $query );
}
DropTables( $server, $uid, $pwd, $tableName1, $tableName2 );
GenerateTables( $server, $uid, $pwd, $dbName, $tableName1, $tableName2 );
?>

View file

@ -0,0 +1,8 @@
<?php
$server = 'TARGET_SERVER';
$databaseName = 'TARGET_DATABASE';
$uid = 'TARGET_USERNAME';
$pwd = 'TARGET_PASSWORD';
?>

View file

@ -0,0 +1,24 @@
--TEST--
a variable bound to a column in a result set
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "SELECT Title, FirstName, EmailPromotion FROM Person.Person where LastName = 'Estes'";
$stmt = $conn->prepare($query);
$stmt->execute();
$stmt->bindColumn('EmailPromotion', $emailpromo);
while ( $row = $stmt->fetch( PDO::FETCH_BOUND ) ){
echo "EmailPromotion: $emailpromo\n";
}
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
EmailPromotion: 2

View file

@ -0,0 +1,38 @@
--TEST--
after a variable is bound, changing the value changes the value passed in the query
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = ?");
$stmt->bindParam(1, $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "Result: "."$row[Name]\n\n";
}
$stmt = null;
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = :contact");
$stmt->bindParam(':contact', $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "Result: "."$row[Name]\n\n";
}
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
Result: Owner
Result: Owner

View file

@ -0,0 +1,22 @@
--TEST--
accesses an output parameter
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$input1 = 'bb';
$stmt = $conn->prepare("select ? = count(* ) from Person.Person");
$stmt->bindParam( 1, $input1, PDO::PARAM_STR, 10);
$stmt->execute();
echo "Result: ".$input1;
//free the statement and connection
$conn = null;
$stmt = null;
?>
--EXPECT--
Result: 19972

View file

@ -0,0 +1,23 @@
--TEST--
uses an input/output parameter
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$dbh = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$dbh->query("IF OBJECT_ID('dbo.sp_ReverseString', 'P') IS NOT NULL DROP PROCEDURE dbo.sp_ReverseString");
$dbh->query("CREATE PROCEDURE dbo.sp_ReverseString @String as VARCHAR(2048) OUTPUT as SELECT @String = REVERSE(@String)");
$stmt = $dbh->prepare("EXEC dbo.sp_ReverseString ?");
$string = "123456789";
$stmt->bindParam(1, $string, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 2048);
$stmt->execute();
print "Result: ".$string; // Expect 987654321
//free the statement and connection
$stmt = null;
$dbh = null;
?>
--EXPECT--
Result: 987654321

View file

@ -0,0 +1,38 @@
--TEST--
after a value $contact is bound, changing the value does not change the value passed in the query
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = ?");
$stmt->bindValue(1, $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "Name: $row[Name]\n\n";
}
$stmt = null;
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = :contact");
$stmt->bindValue(':contact', $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "Name: $row[Name]\n\n";
}
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Name: Sales Agent
Name: Sales Agent

View file

@ -0,0 +1,50 @@
--TEST--
closes the cursor
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd", array('MultipleActiveResultSets' => false ) );
$stmt = $conn->prepare('SELECT * FROM Person.ContactType');
$stmt2 = $conn->prepare('SELECT * FROM HumanResources.Department');
$stmt->execute();
$result = $stmt->fetch();
print_r($result);
$stmt->closeCursor();
$stmt2->execute();
$result = $stmt2->fetch();
print_r($result);
//free the statements and connection
$stmt=null;
$stmt2=null;
$conn=null;
?>
--EXPECT--
Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
Array
(
[DepartmentID] => 1
[0] => 1
[Name] => Engineering
[1] => Engineering
[GroupName] => Research and Development
[2] => Research and Development
[ModifiedDate] => 2008-04-30 00:00:00.000
[3] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,34 @@
--TEST--
returns the number of columns in a result set for 3 queries
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query );
print $stmt->columnCount(); // 0
echo " columns in the result set\n";
echo "\n";
$stmt->execute();
print $stmt->columnCount();
echo " columns in the result set\n";
echo "\n";
$stmt = $conn->query("select * from HumanResources.Department");
print $stmt->columnCount();
echo " columns in the result set\n";
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
0 columns in the result set
3 columns in the result set
4 columns in the result set

View file

@ -0,0 +1,42 @@
--TEST--
displays a prepared statement
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$param = "Owner";
$stmt = $conn->prepare("select * from Person.ContactType where name = :param");
$stmt->execute(array($param));
$stmt->debugDumpParams();
echo "\n\n";
$stmt = $conn->prepare("select * from Person.ContactType where name = ?");
$stmt->execute(array($param));
$stmt->debugDumpParams();
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
SQL: [52] select * from Person.ContactType where name = :param
Params: 1
Key: Name: [6] :param
paramno=0
name=[6] ":param"
is_param=1
param_type=2
SQL: [47] select * from Person.ContactType where name = ?
Params: 1
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2

View file

@ -0,0 +1,20 @@
--TEST--
shows the error code of a SQL query with a mispelled table
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->prepare('SELECT * FROM Person.Addressx');
$stmt->execute();
echo "Error Code: ";
print $stmt->errorCode();
// free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Error Code: 42S02

View file

@ -0,0 +1,24 @@
--TEST--
reports the error info of a SQL statement with a mispelled table name
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->prepare('SELECT * FROM Person.Addressx');
$stmt->execute();
print_r ($stmt->errorInfo());
// free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECTREGEX--
Array
\(
\[0\] => 42S02
\[1\] => 208
\[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Invalid object name 'Person.Addressx'.
\)

View file

@ -0,0 +1,54 @@
--TEST--
Executes a statement
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query );
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n";
}
echo "\n";
$param = "Owner";
$query = "select * from Person.ContactType where name = ?";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param));
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n";
}
// free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
Export Administrator
International Marketing Manager
Marketing Assistant
Marketing Manager
Marketing Representative
Order Administrator
Owner
Owner/Marketing Assistant
Product Manager
Purchasing Agent
Purchasing Manager
Regional Account Representative
Sales Agent
Sales Associate
Sales Manager
Sales Representative
Owner

View file

@ -0,0 +1,152 @@
--TEST--
fetch with all fetch styles
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
print( "\n---------- PDO::FETCH_CLASS -------------\n" );
$stmt = $conn->query( "select * from HumanResources.Department order by GroupName" );
class cc {
function __construct( $arg ) {
echo "$arg";
}
function __toString() {
return $this->DepartmentID . "; " . $this->Name . "; " . $this->GroupName;
}
}
$stmt->setFetchMode(PDO::FETCH_CLASS, 'cc', array( "arg1 " ));
while ( $row = $stmt->fetch(PDO::FETCH_CLASS)) {
print($row . "\n");
}
print( "\n---------- PDO::FETCH_INTO -------------\n" );
$stmt = $conn->query( "select * from HumanResources.Department order by GroupName" );
$c_obj = new cc( '' );
$stmt->setFetchMode(PDO::FETCH_INTO, $c_obj);
while ( $row = $stmt->fetch(PDO::FETCH_INTO)) {
echo "$c_obj\n";
}
print( "\n---------- PDO::FETCH_ASSOC -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetch( PDO::FETCH_ASSOC );
print_r( $result );
print( "\n---------- PDO::FETCH_NUM -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetch( PDO::FETCH_NUM );
print_r ($result );
print( "\n---------- PDO::FETCH_BOTH -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetch( PDO::FETCH_BOTH );
print_r( $result );
print( "\n---------- PDO::FETCH_LAZY -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetch( PDO::FETCH_LAZY );
print_r( $result );
print( "\n---------- PDO::FETCH_OBJ -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetch( PDO::FETCH_OBJ );
print $result->Name;
print( "\n \n" );
print( "\n---------- PDO::FETCH_BOUND -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->bindColumn('Name', $name);
$result = $stmt->fetch( PDO::FETCH_BOUND );
print $name;
print( "\n \n" );
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
---------- PDO::FETCH_CLASS -------------
arg1 9; Human Resources; Executive General and Administration
arg1 10; Finance; Executive General and Administration
arg1 11; Information Services; Executive General and Administration
arg1 14; Facilities and Maintenance; Executive General and Administration
arg1 16; Executive; Executive General and Administration
arg1 15; Shipping and Receiving; Inventory Management
arg1 5; Purchasing; Inventory Management
arg1 7; Production; Manufacturing
arg1 8; Production Control; Manufacturing
arg1 12; Document Control; Quality Assurance
arg1 13; Quality Assurance; Quality Assurance
arg1 6; Research and Development; Research and Development
arg1 1; Engineering; Research and Development
arg1 2; Tool Design; Research and Development
arg1 3; Sales; Sales and Marketing
arg1 4; Marketing; Sales and Marketing
---------- PDO::FETCH_INTO -------------
9; Human Resources; Executive General and Administration
10; Finance; Executive General and Administration
11; Information Services; Executive General and Administration
14; Facilities and Maintenance; Executive General and Administration
16; Executive; Executive General and Administration
15; Shipping and Receiving; Inventory Management
5; Purchasing; Inventory Management
7; Production; Manufacturing
8; Production Control; Manufacturing
12; Document Control; Quality Assurance
13; Quality Assurance; Quality Assurance
6; Research and Development; Research and Development
1; Engineering; Research and Development
2; Tool Design; Research and Development
3; Sales; Sales and Marketing
4; Marketing; Sales and Marketing
---------- PDO::FETCH_ASSOC -------------
Array
(
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_NUM -------------
Array
(
[0] => 1
[1] => Accounting Manager
[2] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_BOTH -------------
Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_LAZY -------------
PDORow Object
(
[queryString] => select * from Person.ContactType where ContactTypeID < 5
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_OBJ -------------
Accounting Manager
---------- PDO::FETCH_BOUND -------------
Accounting Manager

View file

@ -0,0 +1,156 @@
--TEST--
fetches the rows in a result set in an array
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
print "-----------\n";
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetchAll(PDO::FETCH_BOTH);
print_r( $result );
print "\n-----------\n";
print "-----------\n";
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetchAll(PDO::FETCH_NUM);
print_r( $result );
print "\n-----------\n";
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 1);
print_r( $result );
print "\n-----------\n";
class cc {
function __construct( $arg ) {
echo "$arg\n";
}
function __toString() {
echo "To string\n";
}
};
$stmt = $conn->query( 'SELECT TOP(2) * FROM Person.ContactType' );
$all = $stmt->fetchAll( PDO::FETCH_CLASS, 'cc', array( 'Hi!' ));
var_dump( $all );
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
-----------
Array
(
[0] => Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
[1] => Array
(
[ContactTypeID] => 2
[0] => 2
[Name] => Assistant Sales Agent
[1] => Assistant Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
[2] => Array
(
[ContactTypeID] => 3
[0] => 3
[Name] => Assistant Sales Representative
[1] => Assistant Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
[3] => Array
(
[ContactTypeID] => 4
[0] => 4
[Name] => Coordinator Foreign Markets
[1] => Coordinator Foreign Markets
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
)
-----------
-----------
Array
(
[0] => Array
(
[0] => 1
[1] => Accounting Manager
[2] => 2008-04-30 00:00:00.000
)
[1] => Array
(
[0] => 2
[1] => Assistant Sales Agent
[2] => 2008-04-30 00:00:00.000
)
[2] => Array
(
[0] => 3
[1] => Assistant Sales Representative
[2] => 2008-04-30 00:00:00.000
)
[3] => Array
(
[0] => 4
[1] => Coordinator Foreign Markets
[2] => 2008-04-30 00:00:00.000
)
)
-----------
Array
(
[0] => Accounting Manager
[1] => Assistant Sales Agent
[2] => Assistant Sales Representative
[3] => Coordinator Foreign Markets
)
-----------
Hi!
Hi!
array(2) {
[0]=>
object(cc)#2 (3) {
["ContactTypeID"]=>
string(1) "1"
["Name"]=>
string(18) "Accounting Manager"
["ModifiedDate"]=>
string(23) "2008-04-30 00:00:00.000"
}
[1]=>
object(cc)#4 (3) {
["ContactTypeID"]=>
string(1) "2"
["Name"]=>
string(21) "Assistant Sales Agent"
["ModifiedDate"]=>
string(23) "2008-04-30 00:00:00.000"
}
}

View file

@ -0,0 +1,23 @@
--TEST--
fetches a column in a row
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
while ( $result = $stmt->fetchColumn(1)) {
print($result . "\n");
}
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets

View file

@ -0,0 +1,19 @@
--TEST--
fetches the next row as an object
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$result = $stmt->fetchObject();
print $result->Name;
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Accounting Manager

View file

@ -0,0 +1,36 @@
--TEST--
fetches the next row as an object of a user defined class
--SKIPIF--
--FILE--
<?php
//create class of contactType
//the names of the attributes in the class has to be the same as the column names in the database
class contactTypes{
public $ContactTypeID;
public $Name;
public $ModifiedDate;
// function that increments that contact id by 10
public function upperCaseName(){
return strtoupper($this->Name);
}
}// end of class
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID = 5 " );
$contactTypes = $stmt->fetchObject('contactTypes');
//print the class properties
print $contactTypes->ContactTypeID."\n";
print $contactTypes->upperCaseName()."\n";
print $contactTypes->ModifiedDate;
// close the database connection
$stmt=null;
$conn=null;
?>
--EXPECT--
5
EXPORT ADMINISTRATOR
2008-04-30 00:00:00.000

View file

@ -0,0 +1,43 @@
--TEST--
retrieves metadata for a column
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt = $conn->query("select * from Person.ContactType");
$metadata = $stmt->getColumnMeta(2);
var_dump($metadata);
print $metadata['sqlsrv:decl_type'] . "\n";
print $metadata['native_type'] . "\n";
print $metadata['name'];
// free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(8) "datetime"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(12) "ModifiedDate"
["len"]=>
int(23)
["precision"]=>
int(3)
}
datetime
string
ModifiedDate

View file

@ -0,0 +1,350 @@
--TEST--
moves the cursor to the next result set and fetches results
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query1 = "select AddressID from Person.Address where City = 'Bothell'";
$query2 = "select Name from Person.ContactType";
$stmt = $conn->query( $query1 . $query2);
$rowset1 = $stmt->fetchAll();
$stmt->nextRowset();
$rowset2 = $stmt->fetchAll();
var_dump( $rowset1 );
var_dump( $rowset2 );
// free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
array(26) {
[0]=>
array(2) {
["AddressID"]=>
string(1) "5"
[0]=>
string(1) "5"
}
[1]=>
array(2) {
["AddressID"]=>
string(2) "11"
[0]=>
string(2) "11"
}
[2]=>
array(2) {
["AddressID"]=>
string(1) "6"
[0]=>
string(1) "6"
}
[3]=>
array(2) {
["AddressID"]=>
string(2) "18"
[0]=>
string(2) "18"
}
[4]=>
array(2) {
["AddressID"]=>
string(2) "40"
[0]=>
string(2) "40"
}
[5]=>
array(2) {
["AddressID"]=>
string(1) "1"
[0]=>
string(1) "1"
}
[6]=>
array(2) {
["AddressID"]=>
string(2) "10"
[0]=>
string(2) "10"
}
[7]=>
array(2) {
["AddressID"]=>
string(3) "868"
[0]=>
string(3) "868"
}
[8]=>
array(2) {
["AddressID"]=>
string(2) "19"
[0]=>
string(2) "19"
}
[9]=>
array(2) {
["AddressID"]=>
string(2) "16"
[0]=>
string(2) "16"
}
[10]=>
array(2) {
["AddressID"]=>
string(2) "15"
[0]=>
string(2) "15"
}
[11]=>
array(2) {
["AddressID"]=>
string(2) "12"
[0]=>
string(2) "12"
}
[12]=>
array(2) {
["AddressID"]=>
string(5) "18249"
[0]=>
string(5) "18249"
}
[13]=>
array(2) {
["AddressID"]=>
string(1) "7"
[0]=>
string(1) "7"
}
[14]=>
array(2) {
["AddressID"]=>
string(2) "21"
[0]=>
string(2) "21"
}
[15]=>
array(2) {
["AddressID"]=>
string(1) "8"
[0]=>
string(1) "8"
}
[16]=>
array(2) {
["AddressID"]=>
string(2) "17"
[0]=>
string(2) "17"
}
[17]=>
array(2) {
["AddressID"]=>
string(2) "20"
[0]=>
string(2) "20"
}
[18]=>
array(2) {
["AddressID"]=>
string(5) "26486"
[0]=>
string(5) "26486"
}
[19]=>
array(2) {
["AddressID"]=>
string(1) "3"
[0]=>
string(1) "3"
}
[20]=>
array(2) {
["AddressID"]=>
string(2) "14"
[0]=>
string(2) "14"
}
[21]=>
array(2) {
["AddressID"]=>
string(1) "9"
[0]=>
string(1) "9"
}
[22]=>
array(2) {
["AddressID"]=>
string(2) "13"
[0]=>
string(2) "13"
}
[23]=>
array(2) {
["AddressID"]=>
string(1) "4"
[0]=>
string(1) "4"
}
[24]=>
array(2) {
["AddressID"]=>
string(1) "2"
[0]=>
string(1) "2"
}
[25]=>
array(2) {
["AddressID"]=>
string(3) "834"
[0]=>
string(3) "834"
}
}
array(20) {
[0]=>
array(2) {
["Name"]=>
string(18) "Accounting Manager"
[0]=>
string(18) "Accounting Manager"
}
[1]=>
array(2) {
["Name"]=>
string(21) "Assistant Sales Agent"
[0]=>
string(21) "Assistant Sales Agent"
}
[2]=>
array(2) {
["Name"]=>
string(30) "Assistant Sales Representative"
[0]=>
string(30) "Assistant Sales Representative"
}
[3]=>
array(2) {
["Name"]=>
string(27) "Coordinator Foreign Markets"
[0]=>
string(27) "Coordinator Foreign Markets"
}
[4]=>
array(2) {
["Name"]=>
string(20) "Export Administrator"
[0]=>
string(20) "Export Administrator"
}
[5]=>
array(2) {
["Name"]=>
string(31) "International Marketing Manager"
[0]=>
string(31) "International Marketing Manager"
}
[6]=>
array(2) {
["Name"]=>
string(19) "Marketing Assistant"
[0]=>
string(19) "Marketing Assistant"
}
[7]=>
array(2) {
["Name"]=>
string(17) "Marketing Manager"
[0]=>
string(17) "Marketing Manager"
}
[8]=>
array(2) {
["Name"]=>
string(24) "Marketing Representative"
[0]=>
string(24) "Marketing Representative"
}
[9]=>
array(2) {
["Name"]=>
string(19) "Order Administrator"
[0]=>
string(19) "Order Administrator"
}
[10]=>
array(2) {
["Name"]=>
string(5) "Owner"
[0]=>
string(5) "Owner"
}
[11]=>
array(2) {
["Name"]=>
string(25) "Owner/Marketing Assistant"
[0]=>
string(25) "Owner/Marketing Assistant"
}
[12]=>
array(2) {
["Name"]=>
string(15) "Product Manager"
[0]=>
string(15) "Product Manager"
}
[13]=>
array(2) {
["Name"]=>
string(16) "Purchasing Agent"
[0]=>
string(16) "Purchasing Agent"
}
[14]=>
array(2) {
["Name"]=>
string(18) "Purchasing Manager"
[0]=>
string(18) "Purchasing Manager"
}
[15]=>
array(2) {
["Name"]=>
string(31) "Regional Account Representative"
[0]=>
string(31) "Regional Account Representative"
}
[16]=>
array(2) {
["Name"]=>
string(11) "Sales Agent"
[0]=>
string(11) "Sales Agent"
}
[17]=>
array(2) {
["Name"]=>
string(15) "Sales Associate"
[0]=>
string(15) "Sales Associate"
}
[18]=>
array(2) {
["Name"]=>
string(13) "Sales Manager"
[0]=>
string(13) "Sales Manager"
}
[19]=>
array(2) {
["Name"]=>
string(20) "Sales Representative"
[0]=>
string(20) "Sales Representative"
}
}

View file

@ -0,0 +1,45 @@
--TEST--
returns the number of rows added to a table; returns the number of rows in a result set when you specify a scrollable cursor
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = tempdb", "$uid", "$pwd");
$conn->exec("CREAtE TABLE Table1(col1 VARCHAR(15), col2 VARCHAR(15)) ");
$col1 = 'a';
$col2 = 'b';
$query = "insert into Table1(col1, col2) values(?, ?)";
$stmt = $conn->prepare( $query );
$stmt->execute( array( $col1, $col2 ) );
print $stmt->rowCount();
print " rows affects.";
echo "\n\n";
//revert the insert
$conn->exec("delete from Table1 where col1 = 'a' AND col2 = 'b'");
$conn->exec("DROP TABLE Table1 ");
$conn = null;
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
print $stmt->rowCount();
print " rows in result set.";
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
1 rows affects.
20 rows in result set.

View file

@ -0,0 +1,25 @@
--TEST--
sets the query timeout attribute
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd", array('MultipleActiveResultSets'=>false ) );
$stmt = $conn->prepare('SELECT * FROM Person.ContactType');
echo "Attribute number for ATTR_CURSOR: ".$stmt->getAttribute( constant( "PDO::ATTR_CURSOR" ) );
echo "\n";
$stmt->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 2);
echo "Attribute number for SQLSRV_ATTR_QUERY_TIMEOUT: ".$stmt->getAttribute( constant( "PDO::SQLSRV_ATTR_QUERY_TIMEOUT" ) );
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
Attribute number for ATTR_CURSOR: 0
Attribute number for SQLSRV_ATTR_QUERY_TIMEOUT: 2

View file

@ -0,0 +1,93 @@
--TEST--
specifies the fetch mode before fetching
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$stmt1 = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
while ( $row = $stmt1->fetch()) {
print($row['Name'] . "\n");
}
print( "\n---------- PDO::FETCH_ASSOC -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch();
print_r( $result );
print( "\n---------- PDO::FETCH_NUM -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->setFetchMode(PDO::FETCH_NUM);
$result = $stmt->fetch();
print_r ($result );
print( "\n---------- PDO::FETCH_BOTH -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->setFetchMode(PDO::FETCH_BOTH);
$result = $stmt->fetch();
print_r( $result );
print( "\n---------- PDO::FETCH_LAZY -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->setFetchMode(PDO::FETCH_LAZY);
$result = $stmt->fetch();
print_r( $result );
print( "\n---------- PDO::FETCH_OBJ -------------\n" );
$stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
$stmt->setFetchMode(PDO::FETCH_OBJ);
$result = $stmt->fetch();
print $result->Name;
print( "\n \n" );
//free the statements and connection
$stmt1 = null;
$stmt = null;
$conn = null;
?>
--EXPECT--
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
---------- PDO::FETCH_ASSOC -------------
Array
(
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_NUM -------------
Array
(
[0] => 1
[1] => Accounting Manager
[2] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_BOTH -------------
Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_LAZY -------------
PDORow Object
(
[queryString] => select * from Person.ContactType where ContactTypeID < 5
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
---------- PDO::FETCH_OBJ -------------
Accounting Manager

View file

@ -0,0 +1,30 @@
--TEST--
starts a transaction, insert 2 rows and commit the transaction
--SKIPIF--
--FILE--
<?php
require('connect.inc');
//make connection and create a temporaty table in tempdb
$conn = new PDO( "sqlsrv:Server=$server; Database = tempdb ", "$uid", "$pwd");
$conn->exec("CREAtE TABLE Table1(col1 CHARACTER(1), col2 CHARACTER(1)) ");
$conn->beginTransaction();
$ret = $conn->exec("insert into Table1(col1, col2) values('a', 'b') ");
$ret = $conn->exec("insert into Table1(col1, col2) values('a', 'c') ");
//revert the inserts
$ret = $conn->exec("delete from Table1 where col1 = 'a'");
$conn->commit();
// $conn->rollback();
echo $ret." rows affected";
//drop the created temp table
$conn->exec("DROP TABLE Table1 ");
//free statement and connection
$ret=NULL;
$conn=NULL;
?>
--EXPECT--
2 rows affected

View file

@ -0,0 +1,138 @@
--TEST--
connect to a server and specify a database
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$c = new PDO( "sqlsrv:Server=$server ; Database = $databaseName ", "$uid", "$pwd", array(PDO::SQLSRV_ATTR_DIRECT_QUERY => true));
$query = 'SELECT * FROM Person.ContactType';
$stmt = $c->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
print_r( $row );
}
$stmt=null;
$c = null;
?>
--EXPECT--
Array
(
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 2
[Name] => Assistant Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 3
[Name] => Assistant Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 4
[Name] => Coordinator Foreign Markets
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 5
[Name] => Export Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 6
[Name] => International Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 7
[Name] => Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 8
[Name] => Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 9
[Name] => Marketing Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 10
[Name] => Order Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 11
[Name] => Owner
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 12
[Name] => Owner/Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 13
[Name] => Product Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 14
[Name] => Purchasing Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 15
[Name] => Purchasing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 16
[Name] => Regional Account Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 17
[Name] => Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 18
[Name] => Sales Associate
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 19
[Name] => Sales Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 20
[Name] => Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,139 @@
--TEST--
connect to a server, specifying the database later
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$c = new PDO( "sqlsrv:Server=$server", "$uid", "$pwd");
$c->exec( "USE $databaseName");
$query = 'SELECT * FROM Person.ContactType';
$stmt = $c->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row );
}
$stmt=null;
$c = null;
?>
--EXPECT--
Array
(
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 2
[Name] => Assistant Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 3
[Name] => Assistant Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 4
[Name] => Coordinator Foreign Markets
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 5
[Name] => Export Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 6
[Name] => International Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 7
[Name] => Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 8
[Name] => Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 9
[Name] => Marketing Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 10
[Name] => Order Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 11
[Name] => Owner
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 12
[Name] => Owner/Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 13
[Name] => Product Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 14
[Name] => Purchasing Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 15
[Name] => Purchasing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 16
[Name] => Regional Account Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 17
[Name] => Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 18
[Name] => Sales Associate
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 19
[Name] => Sales Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 20
[Name] => Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,148 @@
--TEST--
connect to a server, setting MARS to false
--SKIPIF--
--FILE--
<?php
require('connect.inc');
// Connect to the local server using Windows Authentication and AdventureWorks database
try {
$conn = new PDO( "sqlsrv:Server=$server ; Database = $databaseName ; MultipleActiveResultSets=false", "$uid", "$pwd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server" );
}
$query = 'SELECT * FROM Person.ContactType';
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row );
}
$stmt=null;
$conn = null;
?>
--EXPECT--
Array
(
[ContactTypeID] => 1
[Name] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 2
[Name] => Assistant Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 3
[Name] => Assistant Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 4
[Name] => Coordinator Foreign Markets
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 5
[Name] => Export Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 6
[Name] => International Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 7
[Name] => Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 8
[Name] => Marketing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 9
[Name] => Marketing Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 10
[Name] => Order Administrator
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 11
[Name] => Owner
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 12
[Name] => Owner/Marketing Assistant
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 13
[Name] => Product Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 14
[Name] => Purchasing Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 15
[Name] => Purchasing Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 16
[Name] => Regional Account Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 17
[Name] => Sales Agent
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 18
[Name] => Sales Associate
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 19
[Name] => Sales Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
)
Array
(
[ContactTypeID] => 20
[Name] => Sales Representative
[ModifiedDate] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,18 @@
--TEST--
reports the error code of querying a misspelled column
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:Server=$server ; Database = $databaseName ", "$uid", "$pwd");
$query = "SELECT * FROM Person.Address where Cityx = 'Essen'";
$conn->query($query);
print $conn->errorCode();
//free the connection
$conn=null;
?>
--EXPECT--
42S22

View file

@ -0,0 +1,26 @@
--TEST--
reports the error info of querying a misspelled column
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:Server=$server ; Database = $databaseName ", "$uid", "$pwd");
$query = "SELECT * FROM Person.Address where Cityx = 'Essen'";
$conn->query($query);
print $conn->errorCode();
echo "\n";
print_r ($conn->errorInfo());
//free the connection
$conn=null;
?>
--EXPECTREGEX--
42S22
Array
\(
\[0\] => 42S22
\[1\] => 207
\[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Invalid column name 'Cityx'.
\)

View file

@ -0,0 +1,24 @@
--TEST--
execute a delete and reports how many rows were deleted
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$c = new PDO( "sqlsrv:Server=$server", "$uid", "$pwd");
$c->exec("use tempdb");
$c->exec("CREAtE TABLE Table1(col1 VARCHAR(100), col2 VARCHAR(100)) ");
$ret = $c->exec("insert into Table1 values('xxxyy', 'yyxx')");
$ret = $c->exec("delete from Table1 where col1 = 'xxxyy'");
echo $ret," rows affected";
$c->exec("DROP TABLE Table1 ");
//free the statement and connection
$ret=null;
$c=null;
?>
--EXPECT--
1 rows affected

View file

@ -0,0 +1,39 @@
--TEST--
shows the PDO::ATR_ERRMODE attribute, before and after changing its value
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:Server=$server ; Database = $databaseName", "$uid", "$pwd");
$attributes1 = array( "ERRMODE" );
foreach ( $attributes1 as $val ) {
echo "PDO::ATTR_$val: ";
var_dump ($conn->getAttribute( constant( "PDO::ATTR_$val" ) ));
}
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$attributes1 = array( "ERRMODE" );
foreach ( $attributes1 as $val ) {
echo "PDO::ATTR_$val: ";
var_dump ($conn->getAttribute( constant( "PDO::ATTR_$val" ) ));
}
// An example using PDO::ATTR_CLIENT_VERSION
print_r($conn->getAttribute( PDO::ATTR_CLIENT_VERSION ));
//free the connection
$conn=null;
?>
--EXPECTREGEX--
PDO::ATTR_ERRMODE: int\(0\)
PDO::ATTR_ERRMODE: int\(2\)
Array
\(
\[DriverDllName\] => msodbcsql[0-9]{2}\.dll|libmsodbcsql-[0-9]{2}\.[0-9]\.so\.[0-9]\.[0-9]
\[DriverODBCVer\] => [0-9]{1,2}\.[0-9]{1,2}
\[DriverVer\] => [0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}
\[ExtensionVer\] => [0-9]\.[0-9]\.[0-9](\-((rc)|(preview))(\.[0-9]+)?)?(\+[0-9]+)?
\)

View file

@ -0,0 +1,13 @@
--TEST--
returns an array of PDO drivers
--SKIPIF--
--FILE--
<?php
print_r(PDO::getAvailableDrivers());
?>
--EXPECT--
Array
(
[0] => sqlsrv
)

View file

@ -0,0 +1,36 @@
--TEST--
prepares a statement with parameter markers and forward-only (server-side) cursor
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = tempdb", "$uid", "$pwd");
$conn->exec("CREAtE TABLE Table1(col1 VARCHAR(100), col2 VARCHAR(100))");
$col1 = 'a';
$col2 = 'b';
$query = "insert into Table1(col1, col2) values(?, ?)";
$stmt = $conn->prepare( $query, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY, PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 1 ) );
$stmt->execute( array( $col1, $col2 ) );
print $stmt->rowCount();
echo " row affected\n";
$query = "insert into Table1(col1, col2) values(:col1, :col2)";
$stmt = $conn->prepare( $query, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY, PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 1 ) );
$stmt->execute( array( ':col1' => $col1, ':col2' => $col2 ) );
print $stmt->rowCount();
echo " row affected\n";
// revert the inserts
$conn->exec("delete from Table1 where col1 = 'a' AND col2 = 'b'");
$conn->exec("DROP TABLE Table1 ");
$stmt = null;
$conn = null;
?>
--EXPECT--
1 row affected
1 row affected

View file

@ -0,0 +1,89 @@
--TEST--
prepares a statement with a client-side cursor
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
echo "\n";
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n";
}
echo "\n..\n";
$row = $stmt->fetch( PDO::FETCH_BOTH, PDO::FETCH_ORI_FIRST );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
print "$row[Name]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT );
print "$row[1]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR );
print "$row[1]..\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, 0 );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_LAST );
print_r($row);
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
Export Administrator
International Marketing Manager
Marketing Assistant
Marketing Manager
Marketing Representative
Order Administrator
Owner
Owner/Marketing Assistant
Product Manager
Purchasing Agent
Purchasing Manager
Regional Account Representative
Sales Agent
Sales Associate
Sales Manager
Sales Representative
..
Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
Assistant Sales Agent
Assistant Sales Representative
Assistant Sales Agent..
Array
(
[0] => 1
[1] => Accounting Manager
[2] => 2008-04-30 00:00:00.000
)
Array
(
[0] => 20
[1] => Sales Representative
[2] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,91 @@
--TEST--
prepares a statement with a client-side cursor and specifies scroll type to buffered
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$stmt->execute();
print $stmt->rowCount();
echo "\n";
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n";
}
echo "\n..\n";
$row = $stmt->fetch( PDO::FETCH_BOTH, PDO::FETCH_ORI_FIRST );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
print "$row[Name]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT );
print "$row[1]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR );
print "$row[1]..\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, 0 );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_LAST );
print_r($row);
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
20
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
Export Administrator
International Marketing Manager
Marketing Assistant
Marketing Manager
Marketing Representative
Order Administrator
Owner
Owner/Marketing Assistant
Product Manager
Purchasing Agent
Purchasing Manager
Regional Account Representative
Sales Agent
Sales Associate
Sales Manager
Sales Representative
..
Array
(
[ContactTypeID] => 1
[0] => 1
[Name] => Accounting Manager
[1] => Accounting Manager
[ModifiedDate] => 2008-04-30 00:00:00.000
[2] => 2008-04-30 00:00:00.000
)
Assistant Sales Agent
Assistant Sales Representative
Assistant Sales Agent..
Array
(
[0] => 1
[1] => Accounting Manager
[2] => 2008-04-30 00:00:00.000
)
Array
(
[0] => 20
[1] => Sales Representative
[2] => 2008-04-30 00:00:00.000
)

View file

@ -0,0 +1,135 @@
--TEST--
default query; query for a column; query with a new class; query into an existing class
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$conn->setAttribute( PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 1 );
$query = 'select * from Person.ContactType';
// simple query
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row['Name'] ."\n" );
}
echo "\n........ query for a column ............\n";
// query for one column
$stmt = $conn->query( $query, PDO::FETCH_COLUMN, 1 );
while ( $row = $stmt->fetch() ){
echo "$row\n";
}
echo "\n........ query with a new class ............\n";
$query = 'select * from HumanResources.Department order by GroupName';
// query with a class
class cc {
function __construct( $arg ) {
echo "$arg";
}
function __toString() {
return $this->DepartmentID . "; " . $this->Name . "; " . $this->GroupName;
}
}
$stmt = $conn->query( $query, PDO::FETCH_CLASS, 'cc', array( "arg1 " ));
while ( $row = $stmt->fetch() ){
echo "$row\n";
}
echo "\n........ query into an existing class ............\n";
$c_obj = new cc( '' );
$stmt = $conn->query( $query, PDO::FETCH_INTO, $c_obj );
while ( $stmt->fetch() ){
echo "$c_obj\n";
}
$stmt = null;
$conn=null;
?>
--EXPECT--
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
Export Administrator
International Marketing Manager
Marketing Assistant
Marketing Manager
Marketing Representative
Order Administrator
Owner
Owner/Marketing Assistant
Product Manager
Purchasing Agent
Purchasing Manager
Regional Account Representative
Sales Agent
Sales Associate
Sales Manager
Sales Representative
........ query for a column ............
Accounting Manager
Assistant Sales Agent
Assistant Sales Representative
Coordinator Foreign Markets
Export Administrator
International Marketing Manager
Marketing Assistant
Marketing Manager
Marketing Representative
Order Administrator
Owner
Owner/Marketing Assistant
Product Manager
Purchasing Agent
Purchasing Manager
Regional Account Representative
Sales Agent
Sales Associate
Sales Manager
Sales Representative
........ query with a new class ............
arg1 9; Human Resources; Executive General and Administration
arg1 10; Finance; Executive General and Administration
arg1 11; Information Services; Executive General and Administration
arg1 14; Facilities and Maintenance; Executive General and Administration
arg1 16; Executive; Executive General and Administration
arg1 15; Shipping and Receiving; Inventory Management
arg1 5; Purchasing; Inventory Management
arg1 7; Production; Manufacturing
arg1 8; Production Control; Manufacturing
arg1 12; Document Control; Quality Assurance
arg1 13; Quality Assurance; Quality Assurance
arg1 6; Research and Development; Research and Development
arg1 1; Engineering; Research and Development
arg1 2; Tool Design; Research and Development
arg1 3; Sales; Sales and Marketing
arg1 4; Marketing; Sales and Marketing
........ query into an existing class ............
9; Human Resources; Executive General and Administration
10; Finance; Executive General and Administration
11; Information Services; Executive General and Administration
14; Facilities and Maintenance; Executive General and Administration
16; Executive; Executive General and Administration
15; Shipping and Receiving; Inventory Management
5; Purchasing; Inventory Management
7; Production; Manufacturing
8; Production Control; Manufacturing
12; Document Control; Quality Assurance
13; Quality Assurance; Quality Assurance
6; Research and Development; Research and Development
1; Engineering; Research and Development
2; Tool Design; Research and Development
3; Sales; Sales and Marketing
4; Marketing; Sales and Marketing

View file

@ -0,0 +1,42 @@
--TEST--
insert with quoted parameters
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = tempdb", "$uid", "$pwd");
$conn->exec("CREAtE TABLE Table1(col1 VARCHAR(15), col2 VARCHAR(15)) ");
$param = 'a \' g';
$param2 = $conn->quote( $param );
$query = "INSERT INTO Table1 VALUES( ?, '1' )";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param));
$query = "INSERT INTO Table1 VALUES( ?, ? )";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param, $param2));
$query = "SELECT * FROM Table1";
$stmt = $conn->query($query);
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row['col1'] ." was inserted\n" );
}
// revert the inserts
$query = "delete from Table1 where col1 = ?";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param));
$conn->exec("DROP TABLE Table1 ");
//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
a ' g was inserted
a ' g was inserted

View file

@ -0,0 +1,29 @@
--TEST--
sets to PDO::ATTR_ERRMODE
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
$attributes1 = array( "ERRMODE" );
foreach ( $attributes1 as $val ) {
echo "PDO::ATTR_$val: ";
var_dump ($conn->getAttribute( constant( "PDO::ATTR_$val" ) ));
}
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$attributes1 = array( "ERRMODE" );
foreach ( $attributes1 as $val ) {
echo "PDO::ATTR_$val: ";
var_dump ($conn->getAttribute( constant( "PDO::ATTR_$val" ) ));
}
//free the connection
$conn=null;
?>
--EXPECT--
PDO::ATTR_ERRMODE: int(0)
PDO::ATTR_ERRMODE: int(2)

View file

@ -0,0 +1,42 @@
--TEST--
sets to PDO::SQLSRV_ATTR_DIRECT_QUERY
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO("sqlsrv:Server=$server", "$uid", "$pwd");
$conn->setAttribute(constant('PDO::SQLSRV_ATTR_DIRECT_QUERY'), true);
$stmt1 = $conn->query("DROP TABLE #php_test_table");
$stmt2 = $conn->query("CREATE TABLE #php_test_table ([c1_int] int, [c2_int] int)");
$v1 = 1;
$v2 = 2;
$stmt3 = $conn->prepare("INSERT INTO #php_test_table (c1_int, c2_int) VALUES (:var1, :var2)");
if ($stmt3) {
$stmt3->bindValue(1, $v1);
$stmt3->bindValue(2, $v2);
if ($stmt3->execute())
echo "Execution succeeded\n";
else
echo "Execution failed\n";
}
else
var_dump($conn->errorInfo());
$stmt4 = $conn->query("DROP TABLE #php_test_table");
// free the statements and connection
$stmt1=null;
$stmt2=null;
$stmt3=null;
$stmt4=null;
$conn=null;
?>
--EXPECT--
Execution succeeded

View file

@ -0,0 +1,61 @@
--TEST--
call a stored procedure and retrieve the errorNumber that is returned
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = $databaseName", "$uid", "$pwd");
// Drop the stored procedure if it already exists
$tsql_dropSP = "IF OBJECT_ID('sp_Test_Double', 'P') IS NOT NULL
DROP PROCEDURE sp_Test_Double";
$stmt = $conn->query($tsql_dropSP);
// Create the stored procedure
$tsql_createSP = "CREATE PROCEDURE sp_Test_Double
@ErrorNumber as float(53) = 0.0 OUTPUT
AS
BEGIN
SET @ErrorNumber = -1.111
SELECT 1, 2, 3
END";
$stmt = $conn->query($tsql_createSP);
// Call the stored procedure
$stmt = $conn->prepare("{CALL sp_Test_Double (?)}");
$errorNumber = 0.0;
$stmt->bindParam(1, $errorNumber, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 20);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_NUM);
$stmt->closeCursor();
print("Error Number: $errorNumber\n\n");
$value = $errorNumber - 2;
print("Error Number minus 2: $value\n\n");
print_r($result);
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
Error Number: -1.111
Error Number minus 2: -3.111
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)

View file

@ -0,0 +1,57 @@
--TEST--
call a stored procedure and retrieve the errorNumber that is returned
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = tempdb", "$uid", "$pwd");
// Drop the stored procedure if it already exists
$tsql_dropSP = "IF OBJECT_ID('sp_Test_Integer', 'P') IS NOT NULL
DROP PROCEDURE sp_Test_Integer";
$stmt = $conn->query($tsql_dropSP);
// Create the stored procedure
$tsql_createSP = "CREATE PROCEDURE sp_Test_Integer
@ErrorNumber AS INT = 0 OUTPUT
AS
BEGIN
SET @ErrorNumber = -1
SELECT 1,2,3
END";
$stmt = $conn->query($tsql_createSP);
// Call the stored procedure
$stmt = $conn->prepare("{CALL sp_Test_Integer (:errornumber)}");
$errorNumber = 0;
$stmt->bindParam('errornumber', $errorNumber, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 4);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_NUM);
$stmt->closeCursor();
print("Error Number: $errorNumber\n\n");
print_r($result);
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
Error Number: -1
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)

View file

@ -0,0 +1,60 @@
--TEST--
call a stored procedure and retrieve the errorString that is returned
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$conn = new PDO( "sqlsrv:server=$server ; Database = tempdb", "$uid", "$pwd");
// Drop the stored procedure if it already exists
$tsql_dropSP = "IF OBJECT_ID('sp_Test_String', 'P') IS NOT NULL
DROP PROCEDURE sp_Test_String";
$stmt = $conn->query($tsql_dropSP);
// Create the stored procedure
$tsql_createSP = "CREATE PROCEDURE sp_Test_String
@ErrorString as varchar(20) OUTPUT
AS
BEGIN
SET @ErrorString = REVERSE(@ErrorString)
SELECT 1,2,3
END";
$stmt = $conn->query($tsql_createSP);
// Call the stored procedure
$stmt = $conn->prepare("{CALL sp_Test_String (?)}");
$errorString = "12345";
$stmt->bindParam(1, $errorString, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 20);
print("Error String: $errorString\n\n");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_NUM);
$stmt->closeCursor();
print("Error String: $errorString\n\n");
print_r($result);
//free the statement and connection
$stmt = null;
$conn = null;
?>
--EXPECT--
Error String: 12345
Error String: 54321
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)

View file

@ -0,0 +1,4 @@
This folder mainly contains tests that are derived from the code examples on
https://docs.microsoft.com/en-us/sql/connect/php/pdo-class
https://docs.microsoft.com/en-us/sql/connect/php/pdostatement-class

106
test/bvt/sqlsrv/break.inc Normal file
View file

@ -0,0 +1,106 @@
<?php
// Set SQL server + user + password
$serverName = getenv('MSSQL_SERVERNAME') ?: "sql-2k14-sp2-1.galaxy.ad";
$username = getenv('MSSQL_USERNAME') ?: "sa";
$password = getenv('MSSQL_PASSWORD') ?: "Moonshine4me";
// Generate unique DB name, example: php_20160817_1471475608267
$databaseName = "php_" . date("Ymd") . "_" . round(microtime(true)*1000);
// Generic table name example: php_20160817_1471475608267.dbo.php_firefly
$tableName1 = $databaseName.".dbo.php_firefly1";
$tableName2 = $databaseName.".dbo.php_firefly2";
$connectionInfo = array( "Database"=>"$databaseName", "username"=>"$username", "password"=>"$password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo );
// CREATE database
$stmt0 = sqlsrv_query($conn, "CREATE DATABASE $databaseName");
// Create table
$sql = "CREATE TABLE $tableName1 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
// Insert data using bind parameters
$sql = "INSERT INTO $tableName1 VALUES (?,?)";
for($t=100; $t<115; $t++) {
$stmt = sqlsrv_prepare($conn, $sql);
$ts = substr(sha1($t),0,5);
$params = array($t,$ts);
sqlsrv_execute($stmt, $params);
}
// Create table
$sql = "CREATE TABLE $tableName2 (c1 INT, c2 VARCHAR(40))";
$stmt = sqlsrv_query($conn, $sql);
// Insert data using bind parameters
$sql = "INSERT INTO $tableName2 VALUES (?,?)";
for($t=200; $t<208; $t++) {
$stmt = sqlsrv_prepare($conn, $sql);
$ts = substr(sha1($t),0,5);
$params = array($t,$ts);
sqlsrv_execute($stmt, $params);
}
sqlsrv_close( $conn );
function RestartConn($serverName)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$restart_string = "$powershell (get-service -ComputerName $serverName -Name mssqlserver).Stop()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
// Wait until the service is fully stopped
while (substr_count($servstring, "Stopped") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
}
$restart_string = "$powershell (get-service -ComputerName $serverName -Name mssqlserver).Start()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
// Wait until the service is fully started
while (substr_count($servstring, "Running") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
}
}
function StopConn($serverName)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$restart_string = "$powershell (get-service -ComputerName $serverName -Name mssqlserver).Stop()";
exec( $restart_string );
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
// Wait until the service is fully stopped
while (substr_count($servstring, "Stopped") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
}
}
function StartConn($serverName)
{
$powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
if (substr_count($servstring, "Running") != 1)
{
$restart_string = "$powershell (get-service -ComputerName $serverName -Name mssqlserver).Start()";
exec( $restart_string );
}
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
// Wait until the service is fully started
while (substr_count($servstring, "Running") != 1)
{
sleep(1);
$servstring = shell_exec("$powershell get-service -ComputerName $serverName -Name mssqlserver");
}
}
?>

88
test/bvt/sqlsrv/break.php Normal file
View file

@ -0,0 +1,88 @@
<?php
require_once("connect.inc");
// Using the tempdb database for two tables specifically constructed
// for the connection resiliency tests
$dbName = "tempdb";
$tableName1 = "test_connres1";
$tableName2 = "test_connres2";
// Generate tables for use with the connection resiliency tests.
// Using generated tables will eventually allow us to put the
// connection resiliency tests on Github, since the integrated testing
// from AppVeyor does not have AdventureWorks.
function GenerateTables( $server, $uid, $pwd, $dbName, $tableName1, $tableName2 )
{
$connectionInfo = array( "Database"=>$dbName, "uid"=>$uid, "pwd"=>$pwd );
$conn = sqlsrv_connect( $server, $connectionInfo );
if ( $conn === false )
{
die ( print_r( sqlsrv_errors() ) );
}
// Create table
$sql = "CREATE TABLE $tableName1 ( c1 INT, c2 VARCHAR(40) )";
$stmt = sqlsrv_query( $conn, $sql );
// Insert data
$sql = "INSERT INTO $tableName1 VALUES ( ?, ? )";
for( $t = 100; $t < 116; $t++ )
{
$ts = substr( sha1( $t ),0,5 );
$params = array( $t,$ts );
$stmt = sqlsrv_prepare( $conn, $sql, $params );
sqlsrv_execute( $stmt );
}
// Create table
$sql = "CREATE TABLE $tableName2 ( c1 INT, c2 VARCHAR(40) )";
$stmt = sqlsrv_query( $conn, $sql );
// Insert data
$sql = "INSERT INTO $tableName2 VALUES ( ?, ? )";
for( $t = 200; $t < 209; $t++ )
{
$ts = substr( sha1( $t ),0,5 );
$params = array( $t,$ts );
$stmt = sqlsrv_prepare( $conn, $sql, $params );
sqlsrv_execute( $stmt );
}
sqlsrv_close( $conn );
}
// Break connection by getting the session ID and killing it.
// Note that breaking a connection and testing reconnection requires a
// TCP/IP protocol connection (as opposed to a Shared Memory protocol).
function BreakConnection( $conn, $conn_break )
{
$stmt1 = sqlsrv_query( $conn, "SELECT @@SPID" );
if ( sqlsrv_fetch( $stmt1 ) )
{
$spid=sqlsrv_get_field( $stmt1, 0 );
}
$stmt2 = sqlsrv_prepare( $conn_break, "KILL ".$spid );
sqlsrv_execute( $stmt2 );
sleep(1);
}
// Remove the tables generated by GenerateTables
function DropTables( $server, $uid, $pwd, $tableName1, $tableName2 )
{
$connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd );
$conn = sqlsrv_connect( $server, $connectionInfo );
$query="IF OBJECT_ID('tempdb.dbo.$tableName1, 'U') IS NOT NULL DROP TABLE tempdb.dbo.$tableName1";
$stmt=sqlsrv_query( $conn, $query );
$query="IF OBJECT_ID('tempdb.dbo.$tableName2, 'U') IS NOT NULL DROP TABLE tempdb.dbo.$tableName2";
$stmt=sqlsrv_query( $conn, $query );
}
DropTables( $server, $uid, $pwd, $tableName1, $tableName2 );
GenerateTables( $server, $uid, $pwd, $dbName, $tableName1, $tableName2 );
?>

View file

@ -0,0 +1,31 @@
<?php
$server = 'TARGET_SERVER';
$databaseName = 'TARGET_DATABASE';
$uid = 'TARGET_USERNAME';
$pwd = 'TARGET_PASSWORD';
// RevisionNumber in SalesOrderHeader is subject to a trigger incrementing it whenever
// changes are made to SalesOrderDetail. Since RevisionNumber is a tinyint, it can
// overflow quickly if the BVT tests often run. So we change it directly here first
// before it can overflow.
function ResetRevisionNumber( $server, $databaseName, $uid, $pwd )
{
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd );
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
$stmt0 = sqlsrv_query( $conn, "UPDATE Sales.SalesOrderHeader SET RevisionNumber = 2");
if ( !$stmt0 )
{
echo "Resetting the RevisionNumber failed.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn );
}
?>

View file

@ -0,0 +1,74 @@
--TEST--
executes two queries as part of a transaction
--SKIPIF--
--FILE--
<?php
require('connect.inc');
// First, reset the RevisionNumber to make sure it won't overflow
ResetRevisionNumber( $server, $databaseName, $uid, $pwd );
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>$uid, "PWD"=>$pwd);
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initiate transaction. */
/* Exit script if transaction cannot be initiated. */
if ( sqlsrv_begin_transaction( $conn ) === false )
{
echo "Could not begin transaction.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initialize parameter values. */
$orderId = 43659; $qty = 5; $productId = 709;
$offerId = 1; $price = 5.70;
/* Set up and execute the first query. */
$tsql1 = "INSERT INTO Sales.SalesOrderDetail
(SalesOrderID,
OrderQty,
ProductID,
SpecialOfferID,
UnitPrice)
VALUES (?, ?, ?, ?, ?)";
$params1 = array( $orderId, $qty, $productId, $offerId, $price);
$stmt1 = sqlsrv_query( $conn, $tsql1, $params1 );
/* Set up and execute the second query. */
$tsql2 = "UPDATE Production.ProductInventory
SET Quantity = (Quantity - ?)
WHERE ProductID = ?";
$params2 = array($qty, $productId);
$stmt2 = sqlsrv_query( $conn, $tsql2, $params2 );
/* If both queries were successful, commit the transaction. */
/* Otherwise, rollback the transaction. */
if( $stmt1 && $stmt2 )
{
sqlsrv_commit( $conn );
echo "Transaction was committed.\n";
}
else
{
sqlsrv_rollback( $conn );
echo "Transaction was rolled back.\n";
}
/* Revert the changes */
$d_sql = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43659 AND OrderQty=5 AND ProductID=709 AND SpecialOfferID=1 AND Unitprice=5.70";
$stmt3 = sqlsrv_query($conn, $d_sql);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt($stmt3);
sqlsrv_close( $conn);
?>
--EXPECT--
Transaction was committed.

View file

@ -0,0 +1,78 @@
--TEST--
delete in a transaction
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Begin transaction. */
if( sqlsrv_begin_transaction($conn) === false )
{
echo "Could not begin transaction.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set the Order ID. */
$orderId = 43667;
/* Execute operations that are part of the transaction. Commit on
success, roll back on failure. */
if (perform_trans_ops($conn, $orderId))
{
//If commit fails, roll back the transaction.
if(sqlsrv_commit($conn))
{
echo "Transaction committed.\n";
}
else
{
echo "Commit failed - rolling back.\n";
sqlsrv_rollback($conn);
}
}
else
{
"Error in transaction operation - rolling back.\n";
sqlsrv_rollback($conn);
}
/*Free connection resources*/
sqlsrv_close( $conn);
/*---------------- FUNCTION: perform_trans_ops -----------------*/
function perform_trans_ops($conn, $orderId)
{
/* Define query to update inventory based on sales order info. */
$tsql1 = "UPDATE Production.ProductInventory
SET Quantity = Quantity + s.OrderQty
FROM Production.ProductInventory p
JOIN Sales.SalesOrderDetail s
ON s.ProductID = p.ProductID
WHERE s.SalesOrderID = ?";
/* Define the parameters array. */
$params = array($orderId);
/* Execute the UPDATE statement. Return false on failure. */
if( sqlsrv_query( $conn, $tsql1, $params) === false ) return false;
/* Delete the sales order. Return false on failure */
$tsql2 = "DELETE FROM Sales.SalesOrderDetail
WHERE SalesOrderID = ?";
if(sqlsrv_query( $conn, $tsql2, $params) === false ) return false;
/* Return true because all operations were successful. */
return true;
}
?>
--EXPECT--
Transaction committed.

View file

@ -0,0 +1,49 @@
--TEST--
executes a query, then comsumes and counts results until reaches a specified amount. The remaining query results are then discarded.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare and execute the query. */
$tsql = "SELECT OrderQty, UnitPrice FROM Sales.SalesOrderDetail";
$stmt = sqlsrv_prepare( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation.\n";
die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_execute( $stmt ) === false)
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Initialize tracking variables. */
$salesTotal = 0;
$count = 0;
/* Count and display the number of sales that produce revenue
of $100,000. */
while( ($row = sqlsrv_fetch_array( $stmt)) && $salesTotal <=100000)
{
$qty = $row[0];
$price = $row[1];
$salesTotal += ( $price * $qty);
$count++;
}
echo "$count sales accounted for the first $$salesTotal in revenue.\n";
/* Cancel the pending results. The statement can be reused. */
sqlsrv_cancel( $stmt);
?>
--EXPECT--
57 sales accounted for the first $104171.7607 in revenue.

View file

@ -0,0 +1,80 @@
--TEST--
cancels a statement then reuse.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare and execute the query. */
echo "<h4>SELECT : Scrollable => SQLSRV_CURSOR_KEYSET</h4>";
$sql = "SELECT * FROM Person.Address";
// $tsql = "SELECT * FROM HumanResources.Employee";
$params = array();
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$options = array();
$stmt = sqlsrv_query($conn, $sql, $params, $options);
// sqlsrv_execute ( $stmt );
// $stmt = sqlsrv_query( $conn, $sql, array(), array() );
// PRINT RESULT SET
$numRowsPrint = 3;
echo "<h4>Printing first $numRowsPrint rows</h4>";
echo "<p><table cellpadding=3 border=1 cellspacing=4>";
$count = 0;
// while( ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) && $count <$numRowsPrint)
while( ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_NUMERIC)) && $count <$numRowsPrint)
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
/* Cancel the pending results. The statement can be reused. */
sqlsrv_cancel( $stmt);
// PRINT RESULT SET
echo "<h4>SQLSRV_CANCEL + Print next 5 rows of the result set (MUST BE EMPTY)</h4>";
echo "<p><table cellpadding=3 border=1 cellspacing=4>";
while( ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_NUMERIC)) && $count <($numRowsPrint + 5))
{
echo "<p><font color=red>IF sqlsrv_cancel() is executed, YOU SHOUL NOT SEE THIS</font>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
echo "<p><font color=green>Finished successfully</font>";
?>
--EXPECT--
<h4>SELECT : Scrollable => SQLSRV_CURSOR_KEYSET</h4><h4>Printing first 3 rows</h4><p><table cellpadding=3 border=1 cellspacing=4><tr><td>1</td><td>1970 Napa Ct.</td><td></td><td>Bothell</td><td>79</td><td>9AADCB0D-36CF-483F-84D8-585C2D4EC6E9</td></tr><tr><td>2</td><td>9833 Mt. Dias Blv.</td><td></td><td>Bothell</td><td>79</td><td>32A54B9E-E034-4BFB-B573-A71CDE60D8C0</td></tr><tr><td>3</td><td>7484 Roundtree Drive</td><td></td><td>Bothell</td><td>79</td><td>4C506923-6D1B-452C-A07C-BAA6F5B142A4</td></tr></table><h4>SQLSRV_CANCEL + Print next 5 rows of the result set (MUST BE EMPTY)</h4><p><table cellpadding=3 border=1 cellspacing=4></table><p><font color=green>Finished successfully</font>

View file

@ -0,0 +1,35 @@
--TEST--
client information.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
if( $client_info = sqlsrv_client_info( $conn))
{
foreach( $client_info as $key => $value)
{
echo $key.": ".$value."\n";
}
}
else
{
echo "Client info error.\n";
}
/* Close connection resources. */
sqlsrv_close( $conn);
?>
--EXPECTREGEX--
DriverDllName: msodbcsql[0-9]{2}\.dll|libmsodbcsql-[0-9]{2}\.[0-9]\.so\.[0-9]\.[0-9]
DriverODBCVer: [0-9]{1,2}\.[0-9]{1,2}
DriverVer: [0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}
ExtensionVer: [0-9]\.[0-9]\.[0-9](\-((rc)|(preview))(\.[0-9]+)?)?(\+[0-9]+)?

View file

@ -0,0 +1,35 @@
--TEST--
closes a connection.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare and execute the query. */
$tsql = "SELECT OrderQty, UnitPrice FROM Sales.SalesOrderDetail";
$stmt = sqlsrv_prepare( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation.\n";
die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_execute( $stmt ) === false)
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Close the connection. */
sqlsrv_close( $conn);
echo "Connection closed.\n";
?>
--EXPECT--
Connection closed.

View file

@ -0,0 +1,71 @@
--TEST--
executes two queries as part of a transaction. If both queries are successful, the transaction is committed.
--SKIPIF--
?>
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initiate transaction. */
/* Exit script if transaction cannot be initiated. */
if (sqlsrv_begin_transaction( $conn) === false)
{
echo "Could not begin transaction.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initialize parameter values. */
$orderId = 43659; $qty = 5; $productId = 709;
$offerId = 1; $price = 5.70;
/* Set up and execute the first query. */
$tsql1 = "INSERT INTO Sales.SalesOrderDetail
(SalesOrderID,
OrderQty,
ProductID,
SpecialOfferID,
UnitPrice)
VALUES (?, ?, ?, ?, ?)";
$params1 = array( $orderId, $qty, $productId, $offerId, $price);
$stmt1 = sqlsrv_query( $conn, $tsql1, $params1 );
/* Set up and execute the second query. */
$tsql2 = "UPDATE Production.ProductInventory
SET Quantity = (Quantity - ?)
WHERE ProductID = ?";
$params2 = array($qty, $productId);
$stmt2 = sqlsrv_query( $conn, $tsql2, $params2 );
/* If both queries were successful, commit the transaction. */
/* Otherwise, rollback the transaction. */
if( $stmt1 && $stmt2 )
{
sqlsrv_commit( $conn );
echo "Transaction was committed.\n";
}
else
{
sqlsrv_rollback( $conn );
echo "Transaction was rolled back.\n";
}
/* Revert the changes */
$d_sql = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43659 AND OrderQty=5 AND ProductID=709 AND SpecialOfferID=1 AND Unitprice=5.70";
$stmt3 = sqlsrv_query($conn, $d_sql);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt($stmt3);
sqlsrv_close( $conn);
?>
--EXPECT--
Transaction was committed.

View file

@ -0,0 +1,69 @@
--TEST--
disables the default error-handling behaviour using configure
--SKIPIF--
?>
--FILE--
<?php
/* Connect to the local server using Windows Authentication. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* The Transact-SQL PRINT statement can be used to return
informational or warning messages*/
$tsql = "PRINT 'The PRINT statement can be used ";
$tsql .= "to return user-defined warnings.'";
/* Execute the query and print any errors. */
$stmt1 = sqlsrv_query( $conn, $tsql);
if($stmt1 === false)
{
echo "By default, warnings are treated as errors:\n";
/* Dump errors in the error collection. */
print_r(sqlsrv_errors(SQLSRV_ERR_ERRORS));
}
/* Disable warnings as errors behavior. */
sqlsrv_configure("WarningsReturnAsErrors", 0);
/* Execute the same query and print any errors. */
$stmt2 = sqlsrv_query( $conn, $tsql);
if($stmt2 === false)
{
/* Dump errors in the error collection. */
/* Since the warning generated by the query will not be treated as
an error, this block of code will not be executed. */
print_r(sqlsrv_errors(SQLSRV_ERR_ERRORS));
}
else
{
echo "After calling ";
echo "sqlsrv_configure('WarningsReturnAsErrors', 0), ";
echo "warnings are not treated as errors.";
}
/*Close the connection. */
sqlsrv_close($conn);
?>
--EXPECTREGEX--
By default, warnings are treated as errors:
Array
\(
\[0\] => Array
\(
\[0\] => 01000
\[SQLSTATE\] => 01000
\[1\] => 0
\[code\] => 0
\[2\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]The PRINT statement can be used to return user-defined warnings.
\[message\] => \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]The PRINT statement can be used to return user-defined warnings.
\)
\)
After calling sqlsrv_configure\('WarningsReturnAsErrors', 0\), warnings are not treated as errors.

View file

@ -0,0 +1,176 @@
--TEST--
disables the default error-handling behaviour using configure and returns warnings
--SKIPIF--
?>
--FILE--
<?php
/* Turn off the default behavior of treating errors as warnings.
Note: Turning off the default behavior is done here for demonstration
purposes only. If setting the configuration fails, display errors and
exit the script. */
if( sqlsrv_configure("WarningsReturnAsErrors", 0) === false)
{
DisplayErrors();
die;
}
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
/* If the connection fails, display errors and exit the script. */
if( $conn === false )
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Revert previous updates */
$r_sql="UPDATE HumanResources.Employee SET VacationHours=61 WHERE BusinessEntityID=7;
UPDATE HumanResources.Employee SET VacationHours=62 WHERE BusinessEntityID=8;
UPDATE HumanResources.Employee SET VacationHours=63 WHERE BusinessEntityID=9;
UPDATE HumanResources.Employee SET VacationHours=7 WHERE BusinessEntityID=11";
$stmt4=sqlsrv_query($conn, $r_sql);
sqlsrv_free_stmt( $stmt4 );
/* Drop the stored procedure if it already exists. */
$tsql1 = "IF OBJECT_ID('SubtractVacationHours', 'P') IS NOT NULL
DROP PROCEDURE SubtractVacationHours";
$stmt1 = sqlsrv_query($conn, $tsql1);
/* If the query fails, display errors and exit the script. */
if( $stmt1 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Free the statement resources. */
sqlsrv_free_stmt( $stmt1 );
/* Create the stored procedure. */
$tsql2 = "CREATE PROCEDURE SubtractVacationHours
@BusinessEntityId int,
@VacationHours smallint OUTPUT
AS
UPDATE HumanResources.Employee
SET VacationHours = VacationHours - @VacationHours
WHERE BusinessEntityId = @BusinessEntityId;
SET @VacationHours = (SELECT VacationHours
FROM HumanResources.Employee
WHERE BusinessEntityId = @BusinessEntityId);
IF @VacationHours < 0
BEGIN
PRINT 'WARNING: Vacation hours are now less than zero.'
END;";
$stmt2 = sqlsrv_query( $conn, $tsql2 );
/* If the query fails, display errors and exit the script. */
if( $stmt2 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Free the statement resources. */
sqlsrv_free_stmt( $stmt2 );
/* Set up the array that maps employee ID to used vacation hours. */
$emp_hrs = array (7=>4, 8=>5, 9=>8, 11=>50);
/* Initialize variables that will be used as parameters. */
$businessEntityId = 0;
$vacationHrs = 0;
/* Set up the parameter array. */
$params = array(
array(&$businessEntityId, SQLSRV_PARAM_IN),
array(&$vacationHrs, SQLSRV_PARAM_INOUT)
);
/* Define and prepare the query to substract used vacation hours. */
$tsql3 = "{call SubtractVacationHours(?, ?)}";
$stmt3 = sqlsrv_prepare($conn, $tsql3, $params);
/* If the statement preparation fails, display errors and exit the script. */
if( $stmt3 === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Loop through the employee=>vacation hours array. Update parameter
values before statement execution. */
foreach(array_keys($emp_hrs) as $businessEntityId)
{
$vacationHrs = $emp_hrs[$businessEntityId];
/* Execute the query. If it fails, display the errors. */
if( sqlsrv_execute($stmt3) === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/*Move to the next result returned by the stored procedure. */
if( sqlsrv_next_result($stmt3) === false)
{
DisplayErrors();
die;
}
/* Display any warnings. */
DisplayWarnings();
/* Display updated vacation hours. */
echo "BusinessEntityId $businessEntityId has $vacationHrs ";
echo "remaining vacation hours.\n";
}
/* Free the statement*/
sqlsrv_free_stmt( $stmt3 );
/* close connection resources. */
sqlsrv_close( $conn );
/* ------------- Error Handling Functions --------------*/
function DisplayErrors()
{
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
foreach( $errors as $error )
{
echo "Error: ".$error['message']."\n";
}
}
function DisplayWarnings()
{
$warnings = sqlsrv_errors(SQLSRV_ERR_WARNINGS);
if(!is_null($warnings))
{
foreach( $warnings as $warning )
{
echo "Warning: ".$warning['message']."\n";
}
}
}
?>
--EXPECTREGEX--
Warning: \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Changed database context to 'AdventureWorks2014'.
Warning: \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Changed language setting to us_english.
BusinessEntityId 7 has 57 remaining vacation hours.
BusinessEntityId 8 has 57 remaining vacation hours.
BusinessEntityId 9 has 55 remaining vacation hours.
Error: \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]The UPDATE statement conflicted with the CHECK constraint "CK_Employee_VacationHours". The conflict occurred in database "AdventureWorks2014", table "HumanResources.Employee", column 'VacationHours'.
Error: \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]The statement has been terminated.

View file

@ -0,0 +1,39 @@
--TEST--
creates and opens a connection using Windows Authentication.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare and execute the query. */
$tsql = "SELECT OrderQty, UnitPrice FROM Sales.SalesOrderDetail";
$stmt = sqlsrv_prepare( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation.\n";
die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_execute( $stmt ) === false)
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Close the connection. */
sqlsrv_close( $conn);
?>
--EXPECT--
Connection established.

View file

@ -0,0 +1,29 @@
--TEST--
disables MARS support.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "sql-2k14-sp1-1.galaxy.ad";
$connectionInfo = array( "Database"=>"AdventureWorks2014", "UID"=>"sa", "PWD"=>"Moonshine4me", 'MultipleActiveResultSets'=> false);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Connection established.\n";
}
sqlsrv_close( $conn);
?>
--EXPECT--
Connection established.

View file

@ -0,0 +1,23 @@
--TEST--
specifies to retrieve date and time types as string when connecting.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Connection established.\n";
}
sqlsrv_close( $conn);
?>
--EXPECT--
Connection established.

View file

@ -0,0 +1,38 @@
--TEST--
retrieves dates as string by specifying UTF-8 and ReturnDatesAsStrings when connecting.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd", 'ReturnDatesAsStrings'=> true, "CharacterSet" => 'utf-8');
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT VersionDate FROM AWBuildVersion";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_fetch( $stmt );
// retrieve date as string
$date = sqlsrv_get_field( $stmt, 0 );
if ( $date === false ) {
die( print_r( sqlsrv_errors(), true ));
}
echo $date;
sqlsrv_close( $conn);
?>
--EXPECT--
2014-02-20 04:26:00.000

View file

@ -0,0 +1,83 @@
--TEST--
retrieves UTF-8 encoded data by specifying the UTF-8 character set when making the connection
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd", "CharacterSet" => 'UTF-8');
$conn = sqlsrv_connect( $server, $connectionInfo);
if ( $conn === false ) {
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Set up the Transact-SQL query.
//
$tsql1 = "UPDATE Production.ProductReview
SET Comments = ?
WHERE ProductReviewID = ?";
// Set the parameter values and put them in an array. Note that
// $comments is converted to UTF-8 encoding with the PHP function
// utf8_encode to simulate an application that uses UTF-8 encoded data.
//
$reviewID = 3;
$comments = utf8_encode("testing 1, 2, 3, 4. Testing.");
$params1 = array(
array( $comments, null ),
array( $reviewID, null )
);
// Execute the query.
//
$stmt1 = sqlsrv_query($conn, $tsql1, $params1);
if ( $stmt1 === false ) {
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
else {
echo "The update was successfully executed.<br>";
}
// Retrieve the newly updated data.
//
$tsql2 = "SELECT Comments
FROM Production.ProductReview
WHERE ProductReviewID = ?";
// Set up the parameter array.
//
$params2 = array($reviewID);
// Execute the query.
//
$stmt2 = sqlsrv_query($conn, $tsql2, $params2);
if ( $stmt2 === false ) {
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Retrieve and display the data.
//
if ( sqlsrv_fetch($stmt2) ) {
echo "Comments: ";
$data = sqlsrv_get_field( $stmt2, 0 );
echo $data."<br>";
}
else {
echo "Error in fetching data.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Free statement and connection resources.
//
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_close( $conn);
?>
--EXPECT--
The update was successfully executed.<br>Comments: testing 1, 2, 3, 4. Testing.<br>

View file

@ -0,0 +1,40 @@
--TEST--
retrieve date as PHP type with ReturnDatesAsStrings off by default.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT VersionDate FROM AWBuildVersion";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_fetch( $stmt );
// retrieve date as string
$date = sqlsrv_get_field( $stmt, 0 );
if ( $date === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$date_string = date_format( $date, 'jS, F Y' );
echo "Date = $date_string\n";
sqlsrv_close( $conn);
?>
--EXPECT--
Date = 20th, February 2014

View file

@ -0,0 +1,39 @@
--TEST--
displays errors that occur during a failed statement execution
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up a query to select an invalid column name. */
$tsql = "SELECT InvalidColumnName FROM Sales.SalesOrderDetail";
/* Attempt execution. */
/* Execution will fail because of the invalid column name. */
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
if( ($errors = sqlsrv_errors() ) != null)
{
foreach( $errors as $error)
{
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br>";
echo "code: ".$error[ 'code']."<br>";
echo "message: ".$error[ 'message']."<br>";
}
}
}
/* Free connection resources */
sqlsrv_close( $conn);
?>
--EXPECTREGEX--
SQLSTATE: 42S22<br>code: 207<br>message: \[Microsoft\]\[ODBC Driver 1[1-9] for SQL Server\]\[SQL Server\]Invalid column name 'InvalidColumnName'.<br>

View file

@ -0,0 +1,63 @@
--TEST--
executes a statement that updates a field.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false)
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = (?)
WHERE SalesOrderDetailID = (?)";
/* Set up the parameters array. Parameters correspond, in order, to
question marks in $tsql. */
$params = array(5, 10);
/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
if( $stmt )
{
echo "Statement prepared.\n";
}
else
{
echo "Error in preparing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.\n";
}
else
{
echo "Error in executing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/*revert the update */
$r_sql = "UPDATE Sales.SalesOrderDetail SET OrderQty=6 WHERE SalesOrderDetailID=10";
$stmt2 = sqlsrv_query($conn, $r_sql);
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt($stmt2);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement prepared.
Statement executed.

View file

@ -0,0 +1,134 @@
--TEST--
execute with datetime type in bind parameters.
--SKIPIF--
?>
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false)
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare with datetime type in parameter. */
$tsql = "SELECT * FROM Sales.CurrencyRate WHERE CurrencyRateDate=(?)";
//Pass in parameters directly
$params = array( '2014-05-31 00:00:00.000');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
$crd='2014-05-31 00:00:00.000';
$stmt = sqlsrv_prepare( $conn, $tsql, array(&$crd));
echo "Datetime Type, Select Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo $row['CurrencyRateID'].", ".date_format($row['CurrencyRateDate'], 'Y-m-d H:i:s')."<br>";
}
echo "<br>";
sqlsrv_free_stmt( $stmt);
/* Prepare with datetime type in parameter. */
$tsql = "UPDATE Sales.CurrencyRate SET FromCurrencyCode='CAD' WHERE CurrencyRateDate=(?)";
//Pass in parameters directly
$params = array( '2011-08-15 00:00:00.000');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
$crd='2011-08-15 00:00:00.000';
$stmt = sqlsrv_prepare( $conn, $tsql, array(&$crd));
echo "Datetime Type, Update Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
echo sqlsrv_rows_affected( $stmt)." rows affected.<br><br>";
sqlsrv_free_stmt( $stmt);
/* Revert the Update*/
$tsql = "UPDATE Sales.CurrencyRate SET FromCurrencyCode='USD' WHERE CurrencyRateDate=(?)";
//Pass in parameters directly
$params = array( '2011-08-15 00:00:00.000');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
$crd='2011-08-15 00:00:00.000';
$stmt = sqlsrv_prepare( $conn, $tsql, array(&$crd));
echo "Datetime Type, Update Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
echo sqlsrv_rows_affected( $stmt)." rows affected.<br>";
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Datetime Type, Select Query:<br>Statement prepared.<br>Statement executed.<br>12425, 2014-05-31 00:00:00<br>12426, 2014-05-31 00:00:00<br>12427, 2014-05-31 00:00:00<br>12428, 2014-05-31 00:00:00<br>12429, 2014-05-31 00:00:00<br>12430, 2014-05-31 00:00:00<br>12431, 2014-05-31 00:00:00<br>12432, 2014-05-31 00:00:00<br>12433, 2014-05-31 00:00:00<br>12434, 2014-05-31 00:00:00<br>13532, 2014-05-31 00:00:00<br>12435, 2014-05-31 00:00:00<br><br>Datetime Type, Update Query:<br>Statement prepared.<br>Statement executed.<br>14 rows affected.<br><br>Datetime Type, Update Query:<br>Statement prepared.<br>Statement executed.<br>14 rows affected.<br>

View file

@ -0,0 +1,152 @@
--TEST--
execute with string type in bind parameters.
--SKIPIF--
?>
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false)
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare with string type in parameter. */
$tsql = "SELECT * FROM Sales.SalesOrderDetail WHERE CarrierTrackingNumber=(?)";
//Pass in parameters directly
$params = array( '8650-4A20-B1');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
//$ctn="8650-4A20-B1";
//$stmt = sqlsrv_prepare( $conn, $tsql, array(&$ctn));
echo "String Type, Select Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
$soID = 0;
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo $row['SalesOrderID'].", ".$row['CarrierTrackingNumber']."<br>";
$soID = $row['SalesOrderID'];
}
echo "<br>";
sqlsrv_free_stmt( $stmt);
/* Prepare with string type in parameter. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty=(?)
WHERE CarrierTrackingNumber=(?)";
// RevisionNumber in SalesOrderHeader is subject to a trigger incrementing it whenever
// changes are made to SalesOrderDetail. Since RevisonNumber is a tinyint, it can
// overflow quickly if this test is often run. So we change it directly here first
// before it can overflow.
$stmt0 = sqlsrv_query( $conn, "UPDATE Sales.SalesOrderHeader SET RevisionNumber = 2 WHERE SalesOrderID = $soID" );
//Pass in parameters directly
$params = array(5, '8650-4A20-B1');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
//$oq=5;
//$ctn="8650-4A20-B1";
//$stmt = sqlsrv_prepare( $conn, $tsql, array(&$oq, &$ctn));
echo "String Type, Update Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
echo sqlsrv_rows_affected( $stmt)." rows affected.<br><br>";
sqlsrv_free_stmt( $stmt);
/* Revert back the Update. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty=(?)
WHERE CarrierTrackingNumber=(?)";
//Pass in parameters directly
$params = array(1, '8650-4A20-B1');
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
//Pass in parameters through reference
//$oq=1;
//$ctn="8650-4A20-B1";
//$stmt = sqlsrv_prepare( $conn, $tsql, array(&$oq, &$ctn));
echo "String Type, Update Query:<br>";
if( $stmt )
{
echo "Statement prepared.<br>";
}
else
{
echo "Error in preparing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Error in executing statement.<br>";
die( print_r( sqlsrv_errors(), true));
}
echo sqlsrv_rows_affected( $stmt)." rows affected.<br>";
sqlsrv_free_stmt( $stmt);
/* Free the statement and connection resources. */
//sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
String Type, Select Query:<br>Statement prepared.<br>Statement executed.<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br>51108, 8650-4A20-B1<br><br>String Type, Update Query:<br>Statement prepared.<br>Statement executed.<br>52 rows affected.<br><br>String Type, Update Query:<br>Statement prepared.<br>Statement executed.<br>52 rows affected.<br>

View file

@ -0,0 +1,63 @@
--TEST--
retrieve a row of data.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. Note that both ReviewerName and
Comments are of SQL Server type nvarchar. */
$tsql = "SELECT ReviewerName, Comments
FROM Production.ProductReview
WHERE ProductReviewID=1";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false)
{
echo "Error in retrieving row.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Note: Fields must be accessed in order.
Get the first field of the row. Note that no return type is
specified. Data will be returned as a string, the default for
a field of type nvarchar.*/
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";
/*Get the second field of the row as a stream.
Because the default return type for a nvarchar field is a
string, the return type must be specified as a stream. */
$stream = sqlsrv_get_field( $stmt, 1,
SQLSRV_PHPTYPE_STREAM( SQLSRV_ENC_CHAR));
while( !feof( $stream ))
{
$str = fread( $stream, 10000);
echo $str;
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
John Smith: I can't believe I'm singing the praises of a pair of socks, but I just came back from a grueling
3-day ride and these socks really helped make the trip a blast. They're lightweight yet really cushioned my feet all day.
The reinforced toe is nearly bullet-proof and I didn't experience any problems with rubbing or blisters like I have with
other brands. I know it sounds silly, but it's always the little stuff (like comfortable feet) that makes or breaks a long trip.
I won't go on another trip without them!

View file

@ -0,0 +1,39 @@
--TEST--
retrieves each row of a result set as an associative array.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$tsql = "SELECT FirstName, LastName
FROM Person.Person
WHERE LastName='Alan'";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false)
{
echo "Error in query preparation/execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve each row as an associative array and display the results.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo $row['LastName'].", ".$row['FirstName']."<br>";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Alan, Alisha<br>Alan, Bob<br>Alan, Cheryl<br>Alan, Jamie<br>Alan, Kari<br>Alan, Kelvin<br>Alan, Meghan<br>Alan, Stanley<br>Alan, Xavier<br>

View file

@ -0,0 +1,61 @@
--TEST--
retrieves each row of a result set as a numerically indexed array.
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the query. */
$tsql = "SELECT ProductID,
UnitPrice,
StockedQty
FROM Purchasing.PurchaseOrderDetail
WHERE StockedQty < 3
AND DueDate='2014-01-29'";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each
iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "ProdID: ".$row[0]."\n";
echo "UnitPrice: ".$row[1]."\n";
echo "StockedQty: ".$row[2]."\n";
echo "-----------------\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement executed.
ProdID: 931
UnitPrice: 34.3455
StockedQty: .00
-----------------
ProdID: 932
UnitPrice: 39.2385
StockedQty: .00
-----------------

View file

@ -0,0 +1,39 @@
--TEST--
retrieves each row of a result set as a PHP object
--SKIPIF--
?>
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$tsql = "SELECT FirstName, LastName
FROM Person.Person
WHERE LastName='Alan'";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in query preparation/execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve each row as a PHP object and display the results.*/
while( $obj = sqlsrv_fetch_object( $stmt))
{
echo $obj->LastName.", ".$obj->FirstName."<br>";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Alan, Alisha<br>Alan, Bob<br>Alan, Cheryl<br>Alan, Jamie<br>Alan, Kari<br>Alan, Kelvin<br>Alan, Meghan<br>Alan, Stanley<br>Alan, Xavier<br>

View file

@ -0,0 +1,108 @@
--TEST--
retrieves each row of a result set as an instance of the Product class defined in the script.
--SKIPIF--
--FILE--
<?php
/* Define the Product class. */
class Product
{
/* Constructor */
public function ProductConstruct($ID)
{
$this->objID = $ID;
}
public $objID;
public $name;
public $StockedQty;
public $SafetyStockLevel;
private $UnitPrice;
function getPrice()
{
return $this->UnitPrice;
}
}
/* Connect to the local server using Windows Authentication, and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the query. */
$tsql = "SELECT Name,
SafetyStockLevel,
StockedQty,
UnitPrice,
Color
FROM Purchasing.PurchaseOrderDetail AS pdo
JOIN Production.Product AS p
ON pdo.ProductID = p.ProductID
WHERE pdo.StockedQty < ?
AND pdo.DueDate= ?";
/* Set the parameter values. */
$params = array(3, '2014-01-29');
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql, $params);
if ( $stmt )
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set, printing a row of data upon each
iteration. Note the following:
1) $product is an instance of the Product class.
2) The $ctorParams parameter is required in the call to
sqlsrv_fetch_object, because the Product class constructor is
explicity defined and requires parameter values.
3) The "Name" property is added to the $product instance because
the existing "name" property does not match.
4) The "Color" property is added to the $product instance
because there is no matching property.
5) The private property "UnitPrice" is populated with the value
of the "UnitPrice" field.*/
$i=0; //Used as the $objID in the Product class constructor.
while( $product = sqlsrv_fetch_object( $stmt, "Product", array($i)))
{
echo "Object ID: ".$product->objID."\n";
echo "Product Name: ".$product->Name."\n";
echo "Stocked Qty: ".$product->StockedQty."\n";
echo "Safety Stock Level: ".$product->SafetyStockLevel."\n";
echo "Product Color: ".$product->Color."\n";
echo "Unit Price: ".$product->getPrice()."\n";
echo "-----------------\n";
$i++;
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement executed.
Object ID:
Product Name: LL Road Tire
Stocked Qty: .00
Safety Stock Level: 500
Product Color:
Unit Price: 34.3455
-----------------
Object ID:
Product Name: ML Road Tire
Stocked Qty: .00
Safety Stock Level: 500
Product Color:
Unit Price: 39.2385
-----------------

View file

@ -0,0 +1,38 @@
--TEST--
creates a statement resource then retrieves and displays the field metadata
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare the statement. */
$tsql = "SELECT ReviewerName, Comments FROM Production.ProductReview";
$stmt = sqlsrv_prepare( $conn, $tsql);
/* Get and display field metadata. */
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata)
{
foreach( $fieldMetadata as $name => $value)
{
echo "$name: $value<br>";
}
echo "<br>";
}
/* Note: sqlsrv_field_metadata can be called on any statement
resource, pre- or post-execution. */
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Name: ReviewerName<br>Type: -9<br>Size: 50<br>Precision: <br>Scale: <br>Nullable: 0<br><br>Name: Comments<br>Type: -9<br>Size: 3850<br>Precision: <br>Scale: <br>Nullable: 1<br><br>

View file

@ -0,0 +1,42 @@
--TEST--
creates a statement resource, executes a simple query, and free all resources associated with the statement
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
$stmt = sqlsrv_query( $conn, "SELECT * FROM Person.Person");
if( $stmt )
{
echo "Statement executed.<br>";
}
else
{
echo "Query could not be executed.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Get and display field metadata. */
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata)
{
foreach( $fieldMetadata as $name => $value)
{
echo "$name: $value<br>";
}
echo "<br>";
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement executed.<br>Name: BusinessEntityID<br>Type: 4<br>Size: <br>Precision: 10<br>Scale: <br>Nullable: 0<br><br>Name: PersonType<br>Type: -8<br>Size: 2<br>Precision: <br>Scale: <br>Nullable: 0<br><br>Name: NameStyle<br>Type: -7<br>Size: <br>Precision: 1<br>Scale: <br>Nullable: 0<br><br>Name: Title<br>Type: -9<br>Size: 8<br>Precision: <br>Scale: <br>Nullable: 1<br><br>Name: FirstName<br>Type: -9<br>Size: 50<br>Precision: <br>Scale: <br>Nullable: 0<br><br>Name: MiddleName<br>Type: -9<br>Size: 50<br>Precision: <br>Scale: <br>Nullable: 1<br><br>Name: LastName<br>Type: -9<br>Size: 50<br>Precision: <br>Scale: <br>Nullable: 0<br><br>Name: Suffix<br>Type: -9<br>Size: 10<br>Precision: <br>Scale: <br>Nullable: 1<br><br>Name: EmailPromotion<br>Type: 4<br>Size: <br>Precision: 10<br>Scale: <br>Nullable: 0<br><br>Name: AdditionalContactInfo<br>Type: -152<br>Size: 0<br>Precision: <br>Scale: <br>Nullable: 1<br><br>Name: Demographics<br>Type: -152<br>Size: 0<br>Precision: <br>Scale: <br>Nullable: 1<br><br>Name: rowguid<br>Type: -11<br>Size: 36<br>Precision: <br>Scale: <br>Nullable: 0<br><br>Name: ModifiedDate<br>Type: 93<br>Size: <br>Precision: 23<br>Scale: 3<br>Nullable: 0<br><br>

View file

@ -0,0 +1,63 @@
--TEST--
retrieves a row of data
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. Note that both ReviewerName and
Comments are of the SQL Server nvarchar type. */
$tsql = "SELECT ReviewerName, Comments
FROM Production.ProductReview
WHERE ProductReviewID=1";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false )
{
echo "Error in retrieving row.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Note: Fields must be accessed in order.
Get the first field of the row. Note that no return type is
specified. Data will be returned as a string, the default for
a field of type nvarchar.*/
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";
/*Get the second field of the row as a stream.
Because the default return type for a nvarchar field is a
string, the return type must be specified as a stream. */
$stream = sqlsrv_get_field( $stmt, 1,
SQLSRV_PHPTYPE_STREAM( SQLSRV_ENC_CHAR));
while( !feof( $stream))
{
$str = fread( $stream, 10000);
echo $str;
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
John Smith: I can't believe I'm singing the praises of a pair of socks, but I just came back from a grueling
3-day ride and these socks really helped make the trip a blast. They're lightweight yet really cushioned my feet all day.
The reinforced toe is nearly bullet-proof and I didn't experience any problems with rubbing or blisters like I have with
other brands. I know it sounds silly, but it's always the little stuff (like comfortable feet) that makes or breaks a long trip.
I won't go on another trip without them!

View file

@ -0,0 +1,73 @@
--TEST--
retrieves datatime as string and nvarchar as stream.
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/*revert inserts from previous tests*/
$d_sql = "DELETE FROM Production.ProductReview WHERE EmailAddress!='john@fourthcoffee.com' AND ProductID=709";
$stmt4 = sqlsrv_query($conn, $d_sql);
/* Set up and execute the query. Note that both ReviewerName and
Comments are of the SQL Server nvarchar type. */
$tsql = "SELECT ReviewerName,
ReviewDate,
Rating,
Comments
FROM Production.ProductReview
WHERE ProductID = ?
ORDER BY ReviewDate DESC";
/* Set the parameter value. */
$productID = 709;
$params = array( $productID);
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the data. The first and third fields are
retrieved according to their default types, strings. The second field
is retrieved as a string with 8-bit character encoding. The fourth
field is retrieved as a stream with 8-bit character encoding.*/
while ( sqlsrv_fetch( $stmt))
{
echo "Name: ".sqlsrv_get_field( $stmt, 0 )."\n";
echo "Date: ".sqlsrv_get_field( $stmt, 1,
SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";
echo "Rating: ".sqlsrv_get_field( $stmt, 2 )."\n";
echo "Comments: ";
$comments = sqlsrv_get_field( $stmt, 3,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
fpassthru( $comments);
echo "\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close($conn);
?>
--EXPECT--
Name: John Smith
Date: 2013-09-18 00:00:00.000
Rating: 5
Comments: I can't believe I'm singing the praises of a pair of socks, but I just came back from a grueling
3-day ride and these socks really helped make the trip a blast. They're lightweight yet really cushioned my feet all day.
The reinforced toe is nearly bullet-proof and I didn't experience any problems with rubbing or blisters like I have with
other brands. I know it sounds silly, but it's always the little stuff (like comfortable feet) that makes or breaks a long trip.
I won't go on another trip without them!

View file

@ -0,0 +1,48 @@
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "sql-2k14-sp1-1.galaxy.ad";
$connectionInfo = array( "Database"=>"AdventureWorks2014", "UID"=>"sa", "PWD"=>"Moonshine4me");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the Transact-SQL query. */
$tsql = "SELECT LargePhoto
FROM Production.ProductPhoto
WHERE ProductPhotoID = ?";
/* Set the parameter values and put them in an array. */
$productPhotoID = 70;
$params = array( $productPhotoID);
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.</br>";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the data.
The return data is retrieved as a binary stream. */
if ( sqlsrv_fetch( $stmt ) )
{
$image = sqlsrv_get_field( $stmt, 0,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
header("Content-Type: image/jpg");
fpassthru($image);
}
else
{
echo "Error in retrieving data.</br>";
die(print_r( sqlsrv_errors(), true));
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

View file

@ -0,0 +1,66 @@
--TEST--
retrieves row as a stream specified as a character stream.
--SKIPIF--
--FILE--
<?php
/*Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the Transact-SQL query. */
$tsql = "SELECT ReviewerName,
CONVERT(varchar(32), ReviewDate, 107) AS [ReviewDate],
Rating,
Comments
FROM Production.ProductReview
WHERE ProductReviewID = ? ";
/* Set the parameter value. */
$productReviewID = 1;
$params = array( $productReviewID);
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the data. The first three fields are retrieved
as strings and the fourth as a stream with character encoding. */
if(sqlsrv_fetch( $stmt ) === false )
{
echo "Error in retrieving row.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "Name: ".sqlsrv_get_field( $stmt, 0 )."\n";
echo "Date: ".sqlsrv_get_field( $stmt, 1 )."\n";
echo "Rating: ".sqlsrv_get_field( $stmt, 2 )."\n";
echo "Comments: ";
$comments = sqlsrv_get_field( $stmt, 3,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
fpassthru($comments);
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Name: John Smith
Date: Sep 18, 2013
Rating: 5
Comments: I can't believe I'm singing the praises of a pair of socks, but I just came back from a grueling
3-day ride and these socks really helped make the trip a blast. They're lightweight yet really cushioned my feet all day.
The reinforced toe is nearly bullet-proof and I didn't experience any problems with rubbing or blisters like I have with
other brands. I know it sounds silly, but it's always the little stuff (like comfortable feet) that makes or breaks a long trip.
I won't go on another trip without them!

View file

@ -0,0 +1,40 @@
--TEST--
retrieves dates as strings by specifying UTF-8 when fetching the string.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd", "ReturnDatesAsStrings" => false);
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT VersionDate FROM AWBuildVersion";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_fetch( $stmt );
// retrieve date as string
$date = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STRING("UTF-8"));
if( $date === false ) {
die( print_r( sqlsrv_errors(), true ));
}
echo $date;
sqlsrv_close( $conn);
?>
--EXPECT--
2014-02-20 04:26:00.000

View file

@ -0,0 +1,24 @@
--TEST--
indicate if the result set has one or more rows.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
$stmt = sqlsrv_query( $conn, "select * from Person.Person where PersonType = 'EM'" , array());
if ($stmt !== NULL) {
$rows = sqlsrv_has_rows( $stmt );
if ($rows === true)
echo "\nthere are rows\n";
else
echo "\nno rows\n";
}
?>
--EXPECT--
there are rows

View file

@ -0,0 +1,151 @@
--TEST--
first result is consumed without calling next_result, the next result is made available by calling next_result
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/*revert inserts from previous tests*/
$d_sql = "DELETE FROM Production.ProductReview WHERE EmailAddress!='john@fourthcoffee.com' AND ProductID=709";
$stmt4 = sqlsrv_query($conn, $d_sql);
/* Drop the stored procedure if it already exists. */
$tsql_dropSP = "IF OBJECT_ID('InsertProductReview', 'P') IS NOT NULL
DROP PROCEDURE InsertProductReview";
$stmt1 = sqlsrv_query( $conn, $tsql_dropSP);
if( $stmt1 === false )
{
echo "Error in executing statement 1.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Create the stored procedure. */
$tsql_createSP = " CREATE PROCEDURE InsertProductReview
@ProductID int,
@ReviewerName nvarchar(50),
@ReviewDate datetime,
@EmailAddress nvarchar(50),
@Rating int,
@Comments nvarchar(3850)
AS
BEGIN
INSERT INTO Production.ProductReview
(ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating,
Comments)
VALUES
(@ProductID,
@ReviewerName,
@ReviewDate,
@EmailAddress,
@Rating,
@Comments);
SELECT * FROM Production.ProductReview
WHERE ProductID = @ProductID;
END";
$stmt2 = sqlsrv_query( $conn, $tsql_createSP);
if( $stmt2 === false)
{
echo "Error in executing statement 2.\n";
die( print_r( sqlsrv_errors(), true));
}
/*-------- The next few steps call the stored procedure. --------*/
/* Define the Transact-SQL query. Use question marks (?) in place of the
parameters to be passed to the stored procedure */
$tsql_callSP = "{call InsertProductReview(?, ?, ?, ?, ?, ?)}";
/* Define the parameter array. */
$productID = 709;
$reviewerName = "Morris Gogh";
$reviewDate = "2008-02-12";
$emailAddress = "customer@email.com";
$rating = 3;
$comments = "[Insert comments here.]";
$params = array(
$productID,
$reviewerName,
$reviewDate,
$emailAddress,
$rating,
$comments
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false)
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "<p>";
/* Consume the first result (rows affected by INSERT query in the
stored procedure) without calling sqlsrv_next_result. */
echo "Rows affectd: ".sqlsrv_rows_affected($stmt3)."-----\n";
echo "<p>";
/* Move to the next result and display results. */
$next_result = sqlsrv_next_result($stmt3);
if( $next_result )
{
echo "<p>";
echo "\nReview information for product ID ".$productID.".---\n";
while( $row = sqlsrv_fetch_array( $stmt3, SQLSRV_FETCH_ASSOC))
{
echo "<br>ReviewerName: ".$row['ReviewerName']."\n";
echo "<br>ReviewDate: ".date_format($row['ReviewDate'],
"M j, Y")."\n";
echo "<br>EmailAddress: ".$row['EmailAddress']."\n";
echo "<br>Rating: ".$row['Rating']."\n\n";
}
}
elseif( is_null($next_result))
{
echo "<p>";
echo "No more results.\n";
}
else
{
echo "Error in moving to next result.\n";
die(print_r(sqlsrv_errors(), true));
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_free_stmt( $stmt3 );
sqlsrv_free_stmt( $stmt4 );
sqlsrv_close( $conn );
?>
--EXPECT--
<p>Rows affectd: 1-----
<p><p>
Review information for product ID 709.---
<br>ReviewerName: John Smith
<br>ReviewDate: Sep 18, 2013
<br>EmailAddress: john@fourthcoffee.com
<br>Rating: 5
<br>ReviewerName: Morris Gogh
<br>ReviewDate: Feb 12, 2008
<br>EmailAddress: customer@email.com
<br>Rating: 3

View file

@ -0,0 +1,111 @@
--TEST--
executes a batch query that retrieves information, insert an entry, then again retrieves information
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Revert inserts from previous msdn_next_result_2 tests */
$d_sql = "DELETE FROM Production.ProductReview WHERE EmailAddress!='laura@treyresearch.net' AND ProductID=798";
$stmt = sqlsrv_query($conn, $d_sql);
/* Define the batch query. */
$tsql = "--Query 1
SELECT ProductID, ReviewerName, Rating
FROM Production.ProductReview
WHERE ProductID=?;
--Query 2
INSERT INTO Production.ProductReview (ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating)
VALUES (?, ?, ?, ?, ?);
--Query 3
SELECT ProductID, ReviewerName, Rating
FROM Production.ProductReview
WHERE ProductID=?;";
/* Assign parameter values and execute the query. */
$params = array(798,
798,
'CustomerName',
'2008-4-15',
'test@customer.com',
3,
798 );
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the first result. */
echo "Query 1 result:\n";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
{
print_r($row);
}
echo "<p>";
/* Move to the next result of the batch query. */
sqlsrv_next_result($stmt);
/* Display the result of the second query. */
echo "Query 2 result:\n";
echo "Rows Affected: ".sqlsrv_rows_affected($stmt)."\n";
echo "<p>";
/* Move to the next result of the batch query. */
sqlsrv_next_result($stmt);
/* Retrieve and display the third result. */
echo "Query 3 result:\n";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
{
print_r($row);
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt );
sqlsrv_close( $conn );
?>
--EXPECT--
Query 1 result:
Array
(
[0] => 798
[1] => Laura Norman
[2] => 5
)
<p>Query 2 result:
Rows Affected: 1
<p>Query 3 result:
Array
(
[0] => 798
[1] => Laura Norman
[2] => 5
)
Array
(
[0] => 798
[1] => CustomerName
[2] => 3
)

View file

@ -0,0 +1,45 @@
--TEST--
retrieve all fields
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Define and execute the query. */
$tsql = "SELECT TOP (3) * FROM HumanResources.Department";
$stmt = sqlsrv_query($conn, $tsql);
if( $stmt === false)
{
echo "Error in executing query.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve the number of fields. */
$numFields = sqlsrv_num_fields( $stmt );
/* Iterate through each row of the result set. */
while( sqlsrv_fetch( $stmt ))
{
/* Iterate through the fields of each row. */
for($i = 0; $i < $numFields; $i++)
{
echo sqlsrv_get_field($stmt, $i,
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." ";
}
echo "<br>";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt );
sqlsrv_close( $conn );
?>
--EXPECT--
1 Engineering Research and Development 2008-04-30 00:00:00.000 <br>2 Tool Design Research and Development 2008-04-30 00:00:00.000 <br>3 Sales Sales and Marketing 2008-04-30 00:00:00.000 <br>

View file

@ -0,0 +1,27 @@
--TEST--
num_rows with a ekyset cursor should work.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$stmt = sqlsrv_query( $conn, "select * from Sales.SalesOrderHeader where CustomerID = 29565" , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
$row_count = sqlsrv_num_rows( $stmt );
if ($row_count === false)
echo "\nerror\n";
else if ($row_count >=0)
echo "\n$row_count\n";
?>
--EXPECT--
8

View file

@ -0,0 +1,36 @@
--TEST--
when there is a batch query, the number of rows is only available when use a client-side cursor.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
// $tsql = "select * from HumanResources.Department";
// Client-side cursor and batch statements
$tsql = "select top 8 * from HumanResources.EmployeePayHistory;select top 2 * from HumanResources.Employee;";
// works
$stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered"));
// fails
// $stmt = sqlsrv_query($conn, $tsql);
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"forward"));
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"static"));
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"keyset"));
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"dynamic"));
$row_count = sqlsrv_num_rows( $stmt );
echo "<p>\nRow count first result set = $row_count<br>";
sqlsrv_next_result($stmt);
$row_count = sqlsrv_num_rows( $stmt );
echo "<p>\nRow count second result set = $row_count<br>";
?>
--EXPECT--
<p>
Row count first result set = 8<br><p>
Row count second result set = 2<br>

View file

@ -0,0 +1,60 @@
--TEST--
Prepares and executes a statement.
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Set up Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = ?
WHERE SalesOrderDetailID = ?";
/* Assign parameter values. */
$param1 = 5;
$param2 = 10;
$params = array( &$param1, &$param2);
/* Prepare the statement. */
if( $stmt = sqlsrv_prepare( $conn, $tsql, $params))
{
echo "Statement prepared.<br>";
}
else
{
echo "Statement could not be prepared.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.<br>";
}
else
{
echo "Statement could not be executed.<br>";
die( print_r( sqlsrv_errors(), true));
}
/*revert the update */
$r_sql = "UPDATE Sales.SalesOrderDetail SET OrderQty=6 WHERE SalesOrderDetailID=10";
$stmt2 = sqlsrv_query($conn, $r_sql);
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt($stmt2);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement prepared.<br>Statement executed.<br>

View file

@ -0,0 +1,107 @@
--TEST--
Prepares a statement and then re-execute it with different parameter values.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Define the parameterized query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = ?
WHERE SalesOrderDetailID = ?";
/* Initialize parameters and prepare the statement. Variables $qty
and $id are bound to the statement, $stmt1. */
$qty = 0; $id = 0;
$stmt1 = sqlsrv_prepare( $conn, $tsql, array( &$qty, &$id));
if( $stmt1 )
{
echo "Statement 1 prepared.<br>";
}
else
{
echo "Error in statement preparation.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the SalesOrderDetailID and OrderQty information. This array
maps the order ID to order quantity in key=>value pairs. */
$orders = array( 20=>10, 21=>20, 22=>30);
/* Execute the statement for each order. */
foreach( $orders as $id => $qty)
{
// Because $id and $qty are bound to $stmt1, their updated
// values are used with each execution of the statement.
if( sqlsrv_execute( $stmt1) === false )
{
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
}
echo "Orders updated.<br>";
/* Free $stmt1 resources. This allows $id and $qty to be bound to a different statement.*/
sqlsrv_free_stmt( $stmt1);
/* Now verify that the results were successfully written by selecting
the newly inserted rows. */
$tsql = "SELECT OrderQty
FROM Sales.SalesOrderDetail
WHERE SalesOrderDetailID = ?";
/* Prepare the statement. Variable $id is bound to $stmt2. */
$stmt2 = sqlsrv_prepare( $conn, $tsql, array( &$id));
if( $stmt2 )
{
echo "Statement 2 prepared.<br>";
}
else
{
echo "Error in statement preparation.<br>";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement for each order. */
foreach( array_keys($orders) as $id)
{
/* Because $id is bound to $stmt2, its updated value
is used with each execution of the statement. */
if( sqlsrv_execute( $stmt2))
{
sqlsrv_fetch( $stmt2);
$quantity = sqlsrv_get_field( $stmt2, 0);
echo "Order $id is for $quantity units.<br>";
}
else
{
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
}
/* revert the update */
$r_sql3 = "UPDATE Sales.SalesOrderDetail SET OrderQty = 2 WHERE SalesOrderDetailID = 20";
$stmt3 = sqlsrv_query($conn, $r_sql3);
$r_sql4 = "UPDATE Sales.SalesOrderDetail SET OrderQty = 3 WHERE SalesOrderDetailID = 21";
$stmt4 = sqlsrv_query($conn, $r_sql4);
$r_sql5 = "UPDATE Sales.SalesOrderDetail SET OrderQty = 2 WHERE SalesOrderDetailID = 22";
$stmt5 = sqlsrv_query($conn, $r_sql5);
/* Free $stmt2 and connection resources. */
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_free_stmt( $stmt4);
sqlsrv_free_stmt( $stmt5);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement 1 prepared.<br>Orders updated.<br>Statement 2 prepared.<br>Order 20 is for 10 units.<br>Order 21 is for 20 units.<br>Order 22 is for 30 units.<br>

View file

@ -0,0 +1,88 @@
--TEST--
binding of variables using prepare function
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "INSERT INTO Sales.SalesOrderDetail (SalesOrderID,
OrderQty,
ProductID,
SpecialOfferID,
UnitPrice)
VALUES (?, ?, ?, ?, ?)";
/* Each sub array here will be a parameter array for a query.
The values in each sub array are, in order, SalesOrderID, OrderQty,
ProductID, SpecialOfferID, UnitPrice. */
$parameters = array( array(43659, 8, 711, 1, 20.19),
array(43660, 6, 762, 1, 419.46),
array(43661, 4, 741, 1, 818.70)
);
/* Initialize parameter values. */
$orderId = 0;
$qty = 0;
$prodId = 0;
$specialOfferId = 0;
$price = 0.0;
/* Prepare the statement. $params is implicitly bound to $stmt. */
$stmt = sqlsrv_prepare( $conn, $tsql, array( &$orderId,
&$qty,
&$prodId,
&$specialOfferId,
&$price));
if( $stmt === false )
{
echo "Statement could not be prepared.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Execute a statement for each set of params in $parameters.
Because $params is bound to $stmt, as the values are changed, the
new values are used in the subsequent execution. */
foreach( $parameters as $params)
{
list($orderId, $qty, $prodId, $specialOfferId, $price) = $params;
if( sqlsrv_execute($stmt) === false )
{
echo "Statement could not be executed.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
/* Verify that the row was successfully inserted. */
echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."\n";
}
}
/* Revert the changes */
$d_sql2 = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43659 AND OrderQty=8 AND ProductID=711 AND SpecialOfferID=1 AND Unitprice=20.19";
$stmt2 = sqlsrv_query($conn, $d_sql2);
$d_sql3 = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43660 AND OrderQty=6 AND ProductID=762 AND SpecialOfferID=1 AND Unitprice=419.46";
$stmt3 = sqlsrv_query($conn, $d_sql3);
$d_sql4 = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43661 AND OrderQty=4 AND ProductID=741 AND SpecialOfferID=1 AND Unitprice=818.70";
$stmt4 = sqlsrv_query($conn, $d_sql4);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_free_stmt( $stmt4);
sqlsrv_close( $conn);
?>
--EXPECT--
Rows affected: 1
Rows affected: 1
Rows affected: 1

View file

@ -0,0 +1,37 @@
--TEST--
server side cursor specified when preparing
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if ( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "select * from HumanResources.Employee";
$stmt = sqlsrv_prepare( $conn, $tsql, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
if (! $stmt ) {
echo "Statement could not be prepared.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_execute( $stmt);
$row_count = sqlsrv_num_rows( $stmt );
if ($row_count)
echo "\nRow count = $row_count\n";
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
if ($row ) {
$EmployeeID = sqlsrv_get_field( $stmt, 0);
echo "Employee ID = $EmployeeID \n";
}
?>
--EXPECT--
Row count = 290
Employee ID = 1

View file

@ -0,0 +1,55 @@
--TEST--
Query insert into a table
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the parameterized query. */
$tsql = "INSERT INTO Sales.SalesOrderDetail
(SalesOrderID,
OrderQty,
ProductID,
SpecialOfferID,
UnitPrice,
UnitPriceDiscount)
VALUES
(?, ?, ?, ?, ?, ?)";
/* Set parameter values. */
$params = array(75123, 5, 741, 1, 818.70, 0.00);
// RevisionNumber in SalesOrderHeader is subject to a trigger incrementing it whenever
// changes are made to SalesOrderDetail. Since RevisonNumber is a tinyint, it can
// overflow quickly if this test is often run. So we change it directly here first
// before it can overflow.
$stmt0 = sqlsrv_query( $conn, "UPDATE Sales.SalesOrderHeader SET RevisionNumber = 2 WHERE SalesOrderID = $params[0]");
/* Prepare and execute the query. */
$stmt = sqlsrv_query( $conn, $tsql, $params);
if( $stmt )
{
echo "Row successfully inserted.\n";
}
else
{
echo "Row insertion failed.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Row successfully inserted.

View file

@ -0,0 +1,44 @@
--TEST--
Query update a field in a table
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the parameterized query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = ( ?)
WHERE SalesOrderDetailID = ( ?)";
/* Assign literal parameter values. */
$params = array( 5, 10);
/* Execute the query. */
if( sqlsrv_query( $conn, $tsql, $params))
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/*revert the update */
$r_sql = "UPDATE Sales.SalesOrderDetail SET OrderQty=6 WHERE SalesOrderDetailID=10";
$stmt = sqlsrv_query($conn, $r_sql);
/* Free connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
Statement executed.

View file

@ -0,0 +1,63 @@
--TEST--
updates the quantity in a table, the quantity and product ID are parameters in the UPDATE query.
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the Transact-SQL query.
Use question marks as parameter placeholders. */
$tsql1 = "UPDATE Production.ProductInventory
SET Quantity = ?
WHERE ProductID = ?";
/* Initialize $qty and $productId */
$qty = 10; $productId = 709;
/* Execute the statement with the specified parameter values. */
$stmt1 = sqlsrv_query( $conn, $tsql1, array($qty, $productId));
if( $stmt1 === false )
{
echo "Statement 1 could not be executed.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Free statement resources. */
sqlsrv_free_stmt( $stmt1);
/* Now verify the updated quantity.
Use a question mark as parameter placeholder. */
$tsql2 = "SELECT Quantity
FROM Production.ProductInventory
WHERE ProductID = ?";
/* Execute the statement with the specified parameter value.
Display the returned data if no errors occur. */
$stmt2 = sqlsrv_query( $conn, $tsql2, array($productId));
if( $stmt2 === false )
{
echo "Statement 2 could not be executed.\n";
die( print_r(sqlsrv_errors(), true));
}
else
{
$qty = sqlsrv_fetch_array( $stmt2);
echo "There are $qty[0] of product $productId in inventory.\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt2);
sqlsrv_close( $conn);
?>
--EXPECT--
There are 10 of product 709 in inventory.

View file

@ -0,0 +1,88 @@
--TEST--
queries a call procedure with an in-out parameter.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Drop the stored procedure if it already exists. */
$tsql_dropSP = "IF OBJECT_ID('SubtractVacationHours', 'P') IS NOT NULL
DROP PROCEDURE SubtractVacationHours";
$stmt1 = sqlsrv_query( $conn, $tsql_dropSP);
if( $stmt1 === false )
{
echo "Error in executing statement 1.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Create the stored procedure. */
$tsql_createSP = "CREATE PROCEDURE SubtractVacationHours
@BusinessEntityID int,
@VacationHrs smallint OUTPUT
AS
UPDATE HumanResources.Employee
SET VacationHours = VacationHours - @VacationHrs
WHERE BusinessEntityID = @BusinessEntityID;
SET @VacationHrs = (SELECT VacationHours
FROM HumanResources.Employee
WHERE BusinessEntityID = @BusinessEntityID)";
$stmt2 = sqlsrv_query( $conn, $tsql_createSP);
if( $stmt2 === false )
{
echo "Error in executing statement 2.\n";
die( print_r( sqlsrv_errors(), true));
}
/*--------- The next few steps call the stored procedure. ---------*/
/* Define the Transact-SQL query. Use question marks (?) in place of
the parameters to be passed to the stored procedure */
$tsql_callSP = "{call SubtractVacationHours( ?, ?)}";
/* Define the parameter array. By default, the first parameter is an
INPUT parameter. The second parameter is specified as an INOUT
parameter. Initializing $vacationHrs to 8 sets the returned PHPTYPE to
integer. To ensure data type integrity, output parameters should be
initialized before calling the stored procedure, or the desired
PHPTYPE should be specified in the $params array.*/
$employeeId = 4;
$vacationHrs = 1;
$params = array(
array($employeeId, SQLSRV_PARAM_IN),
array(&$vacationHrs, SQLSRV_PARAM_INOUT)
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false )
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Display the value of the output parameter $vacationHrs. */
sqlsrv_next_result($stmt3);
echo "Remaining vacation hours: ".$vacationHrs;
/* Revert the update in vacation hours */
$r_sql = "UPDATE HumanResources.Employee SET VacationHours=48 WHERE BusinessEntityID=4";
$stmt4 = sqlsrv_query($conn, $r_sql);
/*Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_free_stmt( $stmt4);
sqlsrv_close( $conn);
?>
--EXPECT--
Remaining vacation hours: 47

View file

@ -0,0 +1,82 @@
--TEST--
queries a call to procedure with input and output parameters.
--SKIPIF--
--FILE--
<?php
/* Connect to a server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Drop the stored procedure if it already exists. */
$tsql_dropSP = "IF OBJECT_ID('GetEmployeeSalesYTD', 'P') IS NOT NULL
DROP PROCEDURE GetEmployeeSalesYTD";
$stmt1 = sqlsrv_query( $conn, $tsql_dropSP);
if( $stmt1 === false )
{
echo "Error in executing statement 1.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Create the stored procedure. */
$tsql_createSP = " CREATE PROCEDURE GetEmployeeSalesYTD
@SalesPerson nvarchar(50),
@SalesYTD money OUTPUT
AS
SELECT @SalesYTD = SalesYTD
FROM Sales.SalesPerson AS sp
JOIN HumanResources.vEmployee AS e
ON e.BusinessEntityID = sp.BusinessEntityID
WHERE LastName = @SalesPerson";
$stmt2 = sqlsrv_query( $conn, $tsql_createSP);
if( $stmt2 === false )
{
echo "Error in executing statement 2.\n";
die( print_r( sqlsrv_errors(), true));
}
/*--------- The next few steps call the stored procedure. ---------*/
/* Define the Transact-SQL query. Use question marks (?) in place of
the parameters to be passed to the stored procedure */
$tsql_callSP = "{call GetEmployeeSalesYTD( ?, ? )}";
/* Define the parameter array. By default, the first parameter is an
INPUT parameter. The second parameter is specified as an OUTPUT
parameter. Initializing $salesYTD to 0.0 sets the returned PHPTYPE to
float. To ensure data type integrity, output parameters should be
initialized before calling the stored procedure, or the desired
PHPTYPE should be specified in the $params array.*/
$lastName = "Blythe";
$salesYTD = 0.0;
$params = array(
array(&$lastName, SQLSRV_PARAM_IN),
array(&$salesYTD, SQLSRV_PARAM_OUT)
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false )
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Display the value of the output parameter $salesYTD. */
echo "YTD sales for ".$lastName." are ". $salesYTD. ".";
/*Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_close( $conn);
?>
--EXPECT--
YTD sales for Blythe are 3763178.1787.

View file

@ -0,0 +1,74 @@
--TEST--
server side cursor specified when querying
--SKIPIF--
--FILE--
<?php
//$server = "sql-2k14-sp1-1.galaxy.ad";
//$conn = sqlsrv_connect( $server, array( "Database" => "tempdb", "UID"=>"sa", "PWD"=>"Moonshine4me"));
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if ( $conn === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$stmt = sqlsrv_query( $conn, "DROP TABLE dbo.ScrollTest" );
if ( $stmt !== false ) {
sqlsrv_free_stmt( $stmt );
}
$stmt = sqlsrv_query( $conn, "CREATE TABLE ScrollTest (id int, value char(10))" );
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 1, "Row 1" ));
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 2, "Row 2" ));
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 3, "Row 3" ));
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true ));
}
$stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'keyset' ));
// $stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'dynamic' ));
// $stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'static' ));
$rows = sqlsrv_has_rows( $stmt );
if ( $rows != true ) {
die( "Should have rows" );
}
$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_LAST );
$field1 = sqlsrv_get_field( $stmt, 0 );
$field2 = sqlsrv_get_field( $stmt, 1 );
echo "\n$field1 $field2\n";
//$stmt2 = sqlsrv_query( $conn, "delete from ScrollTest where id = 3" );
// or
$stmt2 = sqlsrv_query( $conn, "UPDATE ScrollTest SET id = 4 WHERE id = 3" );
if ( $stmt2 !== false ) {
sqlsrv_free_stmt( $stmt2 );
}
$result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_LAST );
$field1 = sqlsrv_get_field( $stmt, 0 );
$field2 = sqlsrv_get_field( $stmt, 1 );
echo "\n$field1 $field2\n";
sqlsrv_free_stmt( $stmt );
sqlsrv_close( $conn );
?>
--EXPECT--
3 Row 3
4 Row 3

View file

@ -0,0 +1,66 @@
--TEST--
client side buffered cursor
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if ( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "select * from HumanResources.Department";
// Execute the query with client-side cursor.
$stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered"));
if (! $stmt) {
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
// row count is always available with a client-side cursor
$row_count = sqlsrv_num_rows( $stmt );
echo "\nRow count = $row_count\n";
// Move to a specific row in the result set.
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
$EmployeeID = sqlsrv_get_field( $stmt, 0);
echo "Employee ID = $EmployeeID \n";
// Client-side cursor and batch statements
$tsql = "select top 2 * from HumanResources.Employee;Select top 3 * from HumanResources.EmployeePayHistory";
$stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered"));
if (! $stmt) {
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
$row_count = sqlsrv_num_rows( $stmt );
echo "\nRow count for first result set = $row_count\n";
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
$EmployeeID = sqlsrv_get_field( $stmt, 0);
echo "Employee ID = $EmployeeID \n";
sqlsrv_next_result($stmt);
$row_count = sqlsrv_num_rows( $stmt );
echo "\nRow count for second result set = $row_count\n";
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST);
$BusinessEntityID = sqlsrv_get_field( $stmt, 0);
echo "Business Entity ID = $BusinessEntityID \n";
?>
--EXPECT--
Row count = 16
Employee ID = 1
Row count for first result set = 2
Employee ID = 1
Row count for second result set = 3
Business Entity ID = 3

View file

@ -0,0 +1,87 @@
--TEST--
sqlsrv types are specified for the parameters in query.
--SKIPIF--
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the query. */
$tsql1 = "INSERT INTO HumanResources.EmployeePayHistory (BusinessEntityID,
RateChangeDate,
Rate,
PayFrequency)
VALUES (?, ?, ?, ?)";
/* Construct the parameter array. */
$businessEntityId = 6;
$changeDate = "2005-06-07";
$rate = 30;
$payFrequency = 2;
$params1 = array(
array($businessEntityId, null),
array($changeDate, null, null, SQLSRV_SQLTYPE_DATETIME),
array($rate, null, null, SQLSRV_SQLTYPE_MONEY),
array($payFrequency, null, null, SQLSRV_SQLTYPE_TINYINT)
);
/* Execute the INSERT query. */
$stmt1 = sqlsrv_query($conn, $tsql1, $params1);
if( $stmt1 === false )
{
echo "Error in execution of INSERT.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve the newly inserted data. */
/* Define the query. */
$tsql2 = "SELECT BusinessEntityID, RateChangeDate, Rate, PayFrequency
FROM HumanResources.EmployeePayHistory
WHERE BusinessEntityID = ? AND RateChangeDate = ?";
/* Construct the parameter array. */
$params2 = array($businessEntityId, $changeDate);
/*Execute the SELECT query. */
$stmt2 = sqlsrv_query($conn, $tsql2, $params2);
if( $stmt2 === false )
{
echo "Error in execution of SELECT.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the results. */
$row = sqlsrv_fetch_array( $stmt2 );
if( $row === false )
{
echo "Error in fetching data.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "BusinessEntityID: ".$row['BusinessEntityID']."\n";
echo "Change Date: ".date_format($row['RateChangeDate'], "Y-m-d")."\n";
echo "Rate: ".$row['Rate']."\n";
echo "PayFrequency: ".$row['PayFrequency']."\n";
/* Revert the insert */
$d_sql = "delete from HumanResources.EmployeePayHistory where BusinessEntityId=6 and RateChangeDate='2005-06-07 00:00:00.000'";
$stmt = sqlsrv_query($conn, $d_sql);
/* Free statement and connection resources. */
sqlsrv_free_stmt($stmt1);
sqlsrv_free_stmt($stmt2);
sqlsrv_close($conn);
?>
--EXPECT--
BusinessEntityID: 6
Change Date: 2005-06-07
Rate: 30.0000
PayFrequency: 2

View file

@ -0,0 +1,56 @@
--TEST--
insert stream.
--SKIPIF--
?>
--FILE--
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up the Transact-SQL query. */
$tsql = "INSERT INTO Production.ProductReview (ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating,
Comments)
VALUES (?, ?, ?, ?, ?, ?)";
/* Set the parameter values and put them in an array.
Note that $comments is opened as a stream. */
$productID = '709';
$name = 'Customer Name';
$date = date("Y-m-d");
$email = 'customer@name.com';
$rating = 3;
$comments = fopen( "data://text/plain,[ Insert lengthy comment here.]",
"r");
$params = array($productID, $name, $date, $email, $rating, $comments);
/* Execute the query. All stream data is sent upon execution.*/
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "The query was successfully executed.";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
--EXPECT--
The query was successfully executed.

View file

@ -0,0 +1,91 @@
--TEST--
specify the UTF-8 character set when querying
--SKIPIF--
--FILE--
<?php
// Connect to the local server using Windows Authentication and
// specify the AdventureWorks database as the database in use.
//
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if ( $conn === false ) {
echo "Could not connect.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Set up the Transact-SQL query.
//
$tsql1 = "UPDATE Production.ProductReview
SET Comments = ?
WHERE ProductReviewID = ?";
// Set the parameter values and put them in an array. Note that
// $comments is converted to UTF-8 encoding with the PHP function
// utf8_encode to simulate an application that uses UTF-8 encoded data.
//
$reviewID = 3;
$comments = utf8_encode("testing");
$params1 = array(
array($comments,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STRING('UTF-8')
),
array($reviewID)
);
// Execute the query.
//
$stmt1 = sqlsrv_query($conn, $tsql1, $params1);
if ( $stmt1 === false ) {
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
else {
echo "The update was successfully executed.<br>";
}
// Retrieve the newly updated data.
//
$tsql2 = "SELECT Comments
FROM Production.ProductReview
WHERE ProductReviewID = ?";
// Set up the parameter array.
//
$params2 = array($reviewID);
// Execute the query.
//
$stmt2 = sqlsrv_query($conn, $tsql2, $params2);
if ( $stmt2 === false ) {
echo "Error in statement execution.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Retrieve and display the data.
//
if ( sqlsrv_fetch($stmt2) ) {
echo "Comments: ";
$data = sqlsrv_get_field($stmt2,
0,
SQLSRV_PHPTYPE_STRING('UTF-8')
);
echo $data."<br>";
}
else {
echo "Error in fetching data.<br>";
die( print_r( sqlsrv_errors(), true));
}
// Free statement and connection resources.
//
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_close( $conn);
?>
--EXPECT--
The update was successfully executed.<br>Comments: testing<br>

View file

@ -0,0 +1,70 @@
--TEST--
Rolls back the current transaction on the specified connection and returns the connection to the auto-commit mode.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initiate transaction. */
/* Exit script if transaction cannot be initiated. */
if ( sqlsrv_begin_transaction( $conn) === false )
{
echo "Could not begin transaction.\n";
die( print_r( sqlsrv_errors(), true ));
}
/* Initialize parameter values. */
$orderId = 43659; $qty = 5; $productId = 709;
$offerId = 1; $price = 5.70;
/* Set up and execute the first query. */
$tsql1 = "INSERT INTO Sales.SalesOrderDetail
(SalesOrderID,
OrderQty,
ProductID,
SpecialOfferID,
UnitPrice)
VALUES (?, ?, ?, ?, ?)";
$params1 = array( $orderId, $qty, $productId, $offerId, $price);
$stmt1 = sqlsrv_query( $conn, $tsql1, $params1 );
/* Set up and executee the second query. */
$tsql2 = "UPDATE Production.ProductInventory
SET Quantity = (Quantity - ?)
WHERE ProductID = ?";
$params2 = array($qty, $productId);
$stmt2 = sqlsrv_query( $conn, $tsql2, $params2 );
/* If both queries were successful, commit the transaction. */
/* Otherwise, rollback the transaction. */
if( $stmt1 && $stmt2 )
{
sqlsrv_commit( $conn );
echo "Transaction was committed.\n";
}
else
{
sqlsrv_rollback( $conn );
echo "Transaction was rolled back.\n";
}
/* Revert the changes */
$d_sql = "DELETE FROM Sales.SalesOrderDetail WHERE SalesOrderID=43659 AND OrderQty=5 AND ProductID=709 AND SpecialOfferID=1 AND Unitprice=5.70";
$stmt3 = sqlsrv_query($conn, $d_sql);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt($stmt3);
sqlsrv_close( $conn);
?>
--EXPECT--
Transaction was committed.

View file

@ -0,0 +1,59 @@
--TEST--
Returns the number of rows modified by the last statement executed.
--SKIPIF--
--FILE--
<?php
require('connect.inc');
$connectionInfo = array( "Database"=>"$databaseName", "UID"=>"$uid", "PWD"=>"$pwd");
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET SpecialOfferID = ?
WHERE ProductID = ?";
/* Set parameter values. */
$params = array(2, 709);
/* Execute the statement. */
$stmt = sqlsrv_query( $conn, $tsql, $params);
/* Get the number of rows affected and display appropriate message.*/
$rows_affected = sqlsrv_rows_affected( $stmt);
if( $rows_affected === false)
{
echo "Error in calling sqlsrv_rows_affected.\n";
die( print_r( sqlsrv_errors(), true));
}
elseif( $rows_affected == -1)
{
echo "No information available.\n";
}
else
{
echo $rows_affected." rows were updated.\n";
}
/*revert the update */
$r_sql2 = "UPDATE Sales.SalesOrderDetail SET SpecialOfferID=1 WHERE ProductID=709 AND UnitPriceDiscount=0.00";
$stmt2 = sqlsrv_query($conn, $r_sql2);
$r_sql3 = "UPDATE Sales.SalesOrderDetail SET SpecialOfferID=3 WHERE ProductID=709 AND UnitPriceDiscount=0.05";
$stmt3 = sqlsrv_query($conn, $r_sql3);
$r_sql4 = "UPDATE Sales.SalesOrderDetail SET SpecialOfferID=4 WHERE ProductID=709 AND UnitPriceDiscount=0.10";
$stmt4 = sqlsrv_query($conn, $r_sql4);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_free_stmt( $stmt4);
sqlsrv_close( $conn);
?>
--EXPECTREGEX--
[0-9]+ rows were updated.

Some files were not shown because too many files have changed in this diff Show more