当前位置:主页 >> PHP >> 正文
php,try catch例子与php连mmsql数据库二种方法(pdo与odbc)
阅读:1493 输入:2014-05-24 12:11:05

我上周末,二天只吃了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() . "\n";
    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>