我開始學習使用MS SQL Server的PDO。 與此代碼連接:編碼PHP PDO MS SQL服務器
$userDB = 'userBD';
$passwordDB = 'passwordDB';
try {
$DBH = new PDO('mssql:dbname=spr_bank;host=hostname', $userDB, $passwordDB);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "<h1 style='font-weight:bold; color:red;'>Error. Can not connect to Database</h1>";
file_put_contents('PDOErrors.log', date("Y-m-d H:i:s")." - ".$e->getMessage()."\n", FILE_APPEND);
exit();
}
,現在,我從表中選擇數據:
//simple query and binding with results
$query = $DBH->prepare("SELECT name FROM spr_sotrudnik WHERE login = :login AND password = :password");
$login = (isset($_POST['login']) === true) ? $_POST['login'] : '' ; // ? : shorthand for if else
$password = (isset($_POST['password']) === true) ? md5($_POST['password']) : '' ; // ? : shorthand for if else
// bind parameters - avoids SQL injection
$query->bindValue(':login', $login);
$query->bindValue(':password', $password);
//try... if not catch exception
try {
// run the query
$query->execute();
while($rows = $query->fetch()){
echo $rows["name"];
}
}
catch(PDOException $e){
echo $e->getMessage(), $e->getFile(), $e->getLine();
}
當我去到瀏覽器中,我得到這個錯誤:
SQLSTATE[HY000]: General error: 10007 Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. [10007] (severity 5) [(null)]D:\www\sklad_new\inc\auth.php19
當我寫這個查詢 $query = $DBH->prepare(" SELECT CONVERT(VARCHAR(MAX),name) AS s FROM spr_sotrudnik WHERE login = :login AND password = :password");
我沒有e恐怖,但我看到「?????「
我怎樣才能改變這種
你正在使用什麼[驅動程序](http://es1.php.net/manual/en/pdo.drivers.php)? PDO_DBLIB,PDO_ODBC或PDO_SQLSRV? –
我認爲這[驅動程序](http://es1.php.net/manual/en/ref.pdo-dblib.php),但我不確定。我如何檢查它? – Abdukhafiz
對不起,我真的不需要問,因爲你已經顯示了DNS:'mssql:...'。您正在使用[PDO_DBLIB](http://es1.php.net/manual/en/ref.pdo-dblib.connection.php)。 –