#1 cygsd
一直想用SP存取SQLserver2005,经过一天的鼓捣,成了。测试环境:WINDOWS7,SQLserver 2005 sp3,SpeedPHP3
其中:SQLserver登录方式为windows登录
第一步:
在PHP服务器上安装:SQLServerDriverForPHP20,并加载到PHP扩展设置中。
下载地址:http://download.microsoft.com/download/5/5/6/556130D8-338E-443F-ABFF-3E3921DD7425/SQLServerDriverForPHP20.exe
第二步:
安装sql native client 2008
下载地址:http://download.microsoft.com/download/B/6/3/B63CAC7F-44BB-41FA-92A3-CBF71360F022/1033/x86/sqlncli.msi
第三步:
如不想破坏原SpeedPHP环境,就新建一个SpeedPHP测试环境,修改设置如下:
spConfig设置:
'db' => array( // 数据库连接配置
'driver' => 'mssql2005', // 驱动类型
'host' => 'CYGSD\SQL2005', // 数据库地址
'port' => '', // 端口
'login' => '', // 用户名 //测试环境SQLserver登录方式为windows登录,无需“用户名”和“密码”
'password' => '', // 密码
'database' => 'DaYangDB', // 库名称
'prefix' => '', // 表前缀
'persistent' => FALSE, // 是否使用长链接
),
'db_driver_path' => SP_PATH.'/Drivers/mssql2005.php', // 自定义数据库驱动文件地址
第四步:创建SpeedPHP\Drivers\mssql2005.php文件,内容如下:
/////////////////////////////////////////////////////////////////
// SpeedPHP中文PHP框架, Copyright (C) 2008 - 2010 SpeedPHP.com //
/////////////////////////////////////////////////////////////////
/**
* db_sqlsrv sqlsrv2005数据库的驱动支持,修改自SpeedPHP MsSQL数据库驱动
* 文件名:mssql2005.php
*/
class db_mssql2005 { //类名作了修改
/**
* 数据库链接句柄
*/
public $conn;
/**
* 执行的SQL语句记录
*/
public $arrSql;
/**
* 按SQL语句获取记录结果,返回数组
*
* @param sql 执行的SQL语句
*/
public function getArray($sql)
{
if( ! $result = $this->exec($sql) )return FALSE;
if( ! sqlsrv_num_rows($result) )return FALSE; //所有MSSQL函数修改为SQLSRV函数,下同
$rows = array();
while($rows[] = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)){} //原MSSQL_ASSOC,改为SQLSRV_FETCH_ASSOC
sqlsrv_free_stmt($result); //原为mssql_free_result($result)
array_pop($rows);
return $rows;
}
/**
* 返回当前插入记录的主键ID
*/
public function newinsertid()
{
$result = $this->getArray("select @@IDENTITY as sptmp_newinsert_id");
return $result[0]['sptmp_newinsert_id'];
}
/**
* 格式化带limit的SQL语句。
*/
public function setlimit($sql, $limit)
{
//if(!eregi(",", $limit))$limit = '0,'.$limit;
//$sql .= " LIMIT {$limit}";
//return $this->translimit($sql);
return $sql; //不作处理,直接返回,以兼容spModel。sql2005不支持limit,此处舍弃
}
/**
* 执行一个SQL语句
*
* @param sql 需要执行的SQL语句
*/
public function exec($sql)
{
$this->arrSql[] = $sql;
if( $result = sqlsrv_query($this->conn,$sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))){
//原为mssql_query($sql, $this->conn),参数次序有变
return $result;
}else{
spError("{$sql}
执行错误. " ); // 这里舍弃了 mssql_get_last_message(),未找到合适函数
}
}
/**
* 返回影响行数
*/
public function affected_rows()
{
return sqlsrv_rows_affected($this->conn);
}
/**
* 获取数据表结构
*
* @param tbl_name 表名称
*/
public function getTable($tbl_name)
{
$result = $this->getArray("SELECT syscolumns.name FROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id('{$tbl_name}')");
$columns = array();
foreach( $result as $column )$columns[] = array('Field'=>$column['name']);
return $columns;
}
/**
* 构造函数
*
* @param dbConfig 数据库配置
*/
public function __construct($dbConfig) //此函数修改多处
{
if(!function_exists('sqlsrv_connect'))spError('PHP环境未安装sqlsrv函数库!');
$connstr = array("Database"=>$dbConfig['database'],"ConnectionPooling"=>false,"CharacterSet" => "UTF-8");
$this->conn = sqlsrv_connect($dbConfig['host'], $connstr) or spError("SQLserver2005数据库链接错误,或无法找到数据库,请确认链接正常,数据库名称正确!" );
}
/**
* 对特殊字符进行过滤
*
* @param value 值
*/
public function __val_escape($value) {
if(is_null($value))return 'NULL';
if(is_bool($value))return $value ? 1 : 0;
if(is_int($value))return (int)$value;
if(is_float($value))return (float)$value;
if(@get_magic_quotes_gpc())$value = stripslashes($value);
$search=array("\\","\0","\n","\r","\x1a","'",'"');
$replace=array("\\\\","[NULL]","\\n","\\r","\Z","''",'\"');
return '\''.str_replace($search,$replace,$value).'\'';
}
/**
* 析构函数
*/
public function __destruct()
{
if( TRUE != $dbConfig['persistent'] )@sqlsrv_close($this->conn);
}
//function translimit($sql) {...} 此函数舍弃
}
第五步:测试
在SQLserver上新建一个数据库,名字为xiangmu,三个字段:xmID(int,设为自增量),xmName(nchar(50)),xmBianHao(nchar(50))。
创建model\md_xiangmu.php:
class md_xiangmu extends spModel{
var $table='xiangmu';
var $pk='xmID';
}
以下为controller\main.php测试代码:
class main extends spController
{
function index(){
//phpinfo();
//echo "Hello World!
";
if (!function_exists('sqlsrv_query')) die('PHP环境未安装MSSQL2005函数库!');
$serverName = "CYGSD\SQL2005";
$connectionInfo = array("Database"=>"DaYangDB","ConnectionPooling"=>false,"CharacterSet" => "UTF-8");
$conn = sqlsrv_connect( $serverName,$connectionInfo);
if( $conn == false){
echo "连接失败!";
$error=sqlsrv_errors();
dump($error);
}else{
echo "SQLserver 信息:
";
$server_info = sqlsrv_server_info($conn);
dump($server_info);
echo "SQLclient 信息:
";
$client_info= sqlsrv_client_info($conn);
dump($client_info);
echo "使用SQLserver数据库函数采集的数据:
";
$sql = "SELECT * FROM xiangmu";
$data = sqlsrv_query($conn, $sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
while($row = sqlsrv_fetch_array($data,SQLSRV_FETCH_ASSOC)) {dump($row);}
sqlsrv_close($conn);
}
echo "SpeedPHP的输出结果:
";
$datas=array("xmName"=>"测试SpeedPHP的输出结果","xmBianHao"=>"测试号码");
$newid="新建:". spClass("md_xiangmu")->create($datas);
dump($newid);
$list=spClass("md_xiangmu")->findAll();
dump($list);
//spClass("md_xiangmu")->update(array("xmID"=>"9"),$datas);
//$list=spClass("md_xiangmu")->findAll(array("xmID"=>"10"));
//dump($list);
//spClass("md_xiangmu")->delete(array("xmID"=>"9"));
echo "测试结束。
";
}
}
测试输出的结果(节选):
SQLserver 信息:
Array
(
[CurrentDatabase] => DaYangDB
[SQLServerVersion] => 09.00.4035
[SQLServerName] => CYGSD\SQL2005
)
SQLclient 信息:
Array
(
[DriverDllName] => sqlncli10.dll
[DriverODBCVer] => 03.52
[DriverVer] => 10.50.1600
[ExtensionVer] => 1.1.428.1
)
使用SQLserver数据库函数采集的数据:
[0] => Array
(
[xmID] => 1
[xmName] => test test
[xmBianHao] => this is a test
)
.....
Array
(
[xmID] => 32
[xmName] => 测试SpeedPHP的输出结果
[xmBianHao] => 测试号码
)
Array
(
[xmID] => 33
[xmName] => 测试SpeedPHP的输出结果
[xmBianHao] => 测试号码
)
SpeedPHP的输出结果:
Array
(
[0] => Array
(
[xmID] => 1
[xmName] => test test
[xmBianHao] => this is a test
)
.....
[28] => Array
(
[xmID] => 35
[xmName] => 测试SpeedPHP的输出结果
[xmBianHao] => 测试号码
)
[29] => Array
(
[xmID] => 36
[xmName] => 测试SpeedPHP的输出结果
[xmBianHao] => 测试号码
)
)
2010-11-02 21:45:46