php,try catch例子与php连mmsql数据库二种方法(pdo与odbc)

我上周末,二天只吃了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='9211110410693107109';$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","9211110410693107109",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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值