前段时间用PHP 连接MSSQL时本来用的PHP内置函数, 可是到了取字段的时候却出现了问题.
只有的ntext字段都会报错.最后没有办法只有舍弃PHP的内置函数,用ADODB这个数据库工具.
ADODB5操作数据库具休的步骤如下:
1.下载adodb类库 我下载的是adodb5,可以到网上搜索得到.
2.解压后把adodb5放在建立的工程下面(网上说可以放在任意的目录下,然后设置相应参数就可以了,这里我直接放在这个工程下面)
3.写代码,如下 :
1) 连接MySQL:
- <?php
- header("content-type:text/html;charset=gbk");
- include_once 'adodb5/adodb.inc.php';
- $conn = &ADONewConnection('mysql');
- $conn->Connect('192.168.17.8','sa','','db');
- $sql = "select * from tablename";
- $rs = $conn->Execute($sql);
- //print_r($rs);
- while (!$rs->EOF) {
- // 秀出所有字段,$FieldCount() 会传回字段总数
- for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
- print $rs->fields[$i] . " ";
- }
- // 移至下一笔记录
- $rs->MoveNext();
- // 换列
- echo "<br> ";
- }
- ?>
<?php header("content-type:text/html;charset=gbk"); include_once 'adodb5/adodb.inc.php'; $conn = &ADONewConnection('mysql'); $conn->Connect('192.168.17.8','sa','','db'); $sql = "select * from tablename"; $rs = $conn->Execute($sql); //print_r($rs); while (!$rs->EOF) { // 秀出所有字段,$FieldCount() 会传回字段总数 for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) { print $rs->fields[$i] . " "; } // 移至下一笔记录 $rs->MoveNext(); // 换列 echo "<br> "; } ?>
2).连接MSSQL
- <?php
- include_once ('adodb5/adodb.inc.php');
- class adodbMssql {
- private $dbdriver = 'ado_mssql';
- private $server = '192.168.17.85';
- private $user = 'sa';
- private $password = 'sa';
- private $DATABASE = 'timesheet';
- private $myDSN;
- //private $myDSN = "PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={192.168.17.85};DATABASE=timesheet;UID=sa;PWD=sa;";
- private $db;
- function adodbMssql(){
- $this->myDSN = "PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={".$this->server."};DATABASE=".$this->DATABASE.";UID="
- .$this->user.";PWD=".$this->password.";";
- $this->db = ADONewConnection ($this->dbdriver);
- $this->db->debug = true;
- $this->db->charPage = 65001;
- // $db->Connect($server, $user, $password, $database);
- $this->db->Connect($this->myDSN);
- }
- //执行存储过程
- function getCategoryList(){
- $rs = $this->db->Execute ( 'exec DnnForge_NewsArticles_GetCategoryList @ModuleID=413,@ParentID=-1' );
- return $rs;
- }
- //操作表
- function getNewsArticle($num){
- $rs = $this->db->Execute('select top '.$num.' * from DnnForge_NewsArticles_Article order by ArticleID desc');
- return $rs;
- }
- ?>