问题
I used PDO for MYSql before but now i need to use Microsoft SQL Server Driver for PHP.
I found the manual. http://php.net/manual/en/book.sqlsrv.php
I use Example #2 to connect to the new SQL database: http://www.php.net/manual/en/function.sqlsrv-connect.php
How do I 'convert' the following (PDO mysql) to work with sqlsrv:
$username = 'test';
try {
$conn = new PDO('mysql:host=$host;dbname=$database', $username, password);
$stmt = $conn->prepare('SELECT username users WHERE username = :username LIMIT 1');
$stmt->execute(array('username' => $username));
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch();
echo $result['username'];
} else {
echo 'Nothing found.';
die();
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
回答1:
After solving the connections issues, you have to convert the SQL statement written using MySQL SQL dialect to T-SQL dialectal (used by SQL Server).
[1] SQL Server doesn't has the LIMIT
clause. More, this clause is not included in SQL standard. Instead, you can use ROW_NUMBER() function. But, in this case, you don't have to use this function because ...
[2] ... the username
column from users
table should accept only unique values (and, off course, it should be mandatory). So, you have to be sure that you have an unique constraint or an unique index on username
column. This script will check if you have an unique index on username
column:
DECLARE @TableName SYSNAME=N'dbo.users';
DECLARE @ColumnName SYSNAME=N'username';
SELECT *
FROM sys.indexes i
WHERE i.object_id=OBJECT_ID(@TableName)
AND i.is_unique=1
AND EXISTS (
SELECT *
FROM sys.index_columns ic
INNER JOIN sys.columns c ON c.object_id=ic.object_id
AND c.column_id=ic.column_id
WHERE i.object_id=ic.object_id
AND i.index_id=ic.index_id
AND ic.is_included_column=0
AND c.name=@ColumnName
)
AND (
SELECT COUNT(*)
FROM sys.index_columns ic
INNER JOIN sys.columns c ON c.object_id=ic.object_id
AND c.column_id=ic.column_id
WHERE i.object_id=ic.object_id
AND i.index_id=ic.index_id
AND ic.is_included_column=0
)=1;
[3] If you have an unique index on username
column then you don't need to use the LIMIT 1
clause (or ROW_NUMBER()
function) because the SQL statement will return always zero(min.) or one(max.) record(s).
[4] The SQL statement should be SELECT username FROM users WHERE username = :param
.
回答2:
From php.net
PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.
Since you already use PDO for MySQL, you may consider use the PDO driver for MS SQL like dev-null-dweller said. SQLSRV driver is another choice to access MS SQL. But it will require a overhaul of your current PHP code.
Microsoft provide two kinds of PHP drivers for MS SQL:
php_sqlsrv.dll provides a unique set of SQLSRV API specified for MS SQL.
php_pdo_sqlsrv.dll provides a set of mostly PDO compatible API for MS SQL.
Note your PHP version when downloading the driver:
Version 4.0 supports PHP 7.0+ on Windows and Linux
Version 3.2 supports PHP 5.6, 5.5, and 5.4 on Windows
Version 3.1 supports PHP 5.5 and 5.4 on Windows
Version 3.0 supports PHP 5.4 on Windows
And for PHP5.6 Microsoft provide only driver for x86 version. Unofficial X64 driver can find on http://robsphp.blogspot.tw/2012/06/unofficial-microsoft-sql-server-driver.html
After you download the driver. Copy the version you need to your PHP ext folder. And add the driver name into php.ini configurations, for example:
extension=php_pdo_sqlsrv_56_ts.dll
Then restart the web server. You can confirm the PDO driver is install correctly if there is a section pdo_sqlsrv display by the phpinfo() function.
And please note, MS use an different DSN in the PDO constructor. The line
$conn = new PDO('mysql:host=$host;dbname=$database', $username, password);
Should be changed to
$conn = new PDO('sqlsrv:Server=$host;Database=$database', $username, password);
Then you should not meet the "ERROR: could not find driver" again. Hope this help.
来源:https://stackoverflow.com/questions/13109012/from-pdo-to-sqlsrv