php,try catch例とphp接続mmsqlデータベースの2つの方法(pdoとodbc)
1951 ワード
私は先週末、2日に2回しか食事をしなかったので、8時間も寝ていたでしょう.最後にthinkphpをサーバーに変える問題を解決しました.なぜなら、私のapacheバージョンが高すぎて、2.0以下はmssqlをサポートしていないからです.同時にネットでコードを探す時、try catchの使用も勉強しました.
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8'>
<title>/ /</title>
</head>
<body>
<?php
phpinfo();
try {
$hostname = "192.192.192.2"; //host
$dbname = "fymis"; //db name
$username = "asuser"; // username like 'sa'
$pw = "9211110410693107109"; // password for the user
$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "
";
exit;
}
$stmt = $dbh->prepare("SELECT * FROM table");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
$server='192.192.192.2';$username='asuser';$password='921109';$database='fymis';
//
$link="Driver={SQL Server};Server=192.192.192.2;Database=fymis";
$conn=odbc_connect("Driver={SQL Server};Server=192.192.192.2;Database=fymis","asuser","921111109",SQL_CUR_USE_ODBC);
//$conn=odbc_connect($link,$username,$password,SQL_CUR_USE_ODBC);
echo $conn?NULL:"Couldn't connect to SQL Server on ".$server;
$sql1="SELECT * FROM SYSOBJECTS WHERE XTYPE = 'U' order by name";
$sql="SELECT * FROM Employeeinfo where EmployeeNo='001'";
$result=odbc_exec($conn,$sql);
$i=1;
echo '<pre>';
while($r=odbc_fetch_array($result)){
print_r($r);echo "<br>";
$i++;
}
echo '</pre>';
odbc_free_result($result);
odbc_close($conn);
?>
</body>