pdosqlsrv 连接sqlserver数据库

<?php
header("Content-type: text/html; charset=utf-8");

if(PATH_SEPARATOR==':')
    echo '本机操作系统是:Linux<br>';
else
    echo '本机操作系统是:Windows<br>';



  try {
      //服务器
      $host = "127.0.0.1";
      $port = '1433';
      $dbname = "databasename";
      $username = "sa";
      $pw = "xxxwtxxx";
      $dbh = null;
    //  if(PATH_SEPARATOR==':'){
     //     $dbh = new PDO ("dblib:host=$host:$port;dbname=$dbname","$username","$pw");
     // }else{

          $dbh = new PDO("sqlsrv:Server=$host,$port;Database=$dbname",$username,$pw);
     // }


  } catch (PDOException $e) {
      echo "Failed to get DB handle: " . $e->getMessage() . "\n";
      exit;
  }

  $stmt = $dbh->prepare("SELECT top 1 * FROM tablename");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
      var_dump($row);
  }
  unset($dbh); unset($stmt);

转载请注明出处:

转载自http://lxfamn.cn/blog

未经允许不得转载:lxfamn » pdosqlsrv 连接sqlserver数据库

赞 (0) 打赏

置顶推荐

评论 0

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏