Php Mssql操作簡單封裝支援預存程序

來源:互聯網
上載者:User

複製代碼 代碼如下:<?php
/*
* class :Mssql
* time :2009-12-10
* author :Libaochang
* version :1.0b
* description :mssql database access class,it can execute the procedur or sql
*/
class MssqlUtil
{
var $user = null; //database user name
var $keys = null; //database user password
var $host = 'localhost'; //database host name/ip and port
var $base = null; //database name
var $link = null; //create link
/**
* construct function init all parmeters
* @param <type> $host database host name/ip and port
* @param <type> $user database user name
* @param <type> $keys database user password
* @param <type> $base database name
*/
function __construct($host,$user,$keys,$base)
{
$this->host = $host;
$this->user = $user;
$this->keys = $keys;
$this->base = $base;
}
/**
* create the connection
*/
function connect()
{
$this->link = mssql_connect($this->host,$this->user,$this->keys);
if(!$this->link)
{
die('connecting failed...check the module and setting...');
}
$select = mssql_select_db($this->base,$this->link);
if(!$select)
{
die('data base is not exist...,please checke it ...');
}
}
/**
* execute the procedur width the parameter
* @param <type> $pName procedur name
* @param <type> $parName parameters it's like this $par=array('@a'=>'a')
* @param <type> $sqlTyle the procedur's parameter type, it's llike this $sqlType=array(SQLVARCHAR,SQLVARCHAR); and there is not the char single quote mark(').
* @return <type> object array
*/
function executeProcedur($pName,$parName,$sqlTyle)
{
$this->connect();
$stmt = mssql_init($pName,$this->link);
if(isset($parName))
{
$i = 0;
foreach($parName as $par=>$value)
{
mssql_bind($stmt,$par,$value,$sqlTyle[$i]);
++$i;
}
$res = mssql_execute($stmt);
$this->close();
while($row=mssql_fetch_assoc($res))
{
$r[] = $row;
}
unset($i);
mssql_free_result($res);
mssql_free_statement($stmt);
return $r;
}
}
/**
* execute procedur without the parameter
* @param <type> $pName Procedur Name
* @return <type> object array
*/
function executeProcedurNoPar($pName)
{
$this->connect();
$stmt = mssql_init($pName,$this->link);
$res = mssql_execute($stmt);
$this->close();
while($row=mssql_fetch_assoc($res))
{
$r[] = $row;
}
mssql_free_result($res);
mssql_free_statement($stmt);
return $r;
}
/**
* Get one row return Array
* @param <type> $sql
* @return <type> Array
*/
function getRowArray($sql)
{
$res = $this->query($sql);
$r = mssql_fetch_row($res);
mssql_free_result($res);
return $r;
}
/**
* Get one row return object
* @param <type> $sql Sql
* @return <type> Object
*/
function getRowObject($sql)
{
$res = $this->query($sql);
$r = mssql_fetch_assoc($res);
return $r;
}
/**
* Execute one sql
* @param <type> $sql Sql
* @return <type> result
*/
function query($sql)
{
$this->connect();
$res = mssql_query($sql,$this->link);
$this->close();
return $res;
}
/**
* Get every row from result by Object, Return a Array with every element is Object
* @param <type> $sql
* @return <type> Object Array result
*/
function getResult($sql)
{
$res = $this->query($sql);
while($row=mssql_fetch_assoc($res))
{
$r[] = $row;
}
unset($row);
mssql_free_result($res);
return $r;
}
/**
* execute a sql
* @param <type> $sql Sql
*/
function executeSql($sql)
{
return $this->query($sql);
}
/**
* execute a sql statement
* @param <type> $sql
* @return <type> int $affected rows
*/
function querySql($sql)
{
$this->connect();
mssql_query($sql,$this->link);
$affected = mssql_rows_affected($this->link);
$this->close();
return $affected;
}
/**
* close connection
*/
function close()
{
mssql_close();
}
}
?>

下面說下調用 複製代碼 代碼如下:function __autoload($MssqlUtil)
{
require $MssqlUtil.'.php';
}
$db = new MssqlUtil($config['host'],$config['user'],$config['keys'],$config['base']);

主要說下帶參數的預存程序調用 複製代碼 代碼如下:$pName 預存程序名字
$parName 參數,參數形式很重要,是數群組類型,對應關係為
array('@a'=>'a') @a 為預存程序裡面的參數,a為要傳遞的值
$sqlTyle 是預存程序參數的資料類型,是數組形式,也很重要
array(SQLCHAR,SQLVARCHAR),注意不要加單引號等,因為SQLVARCHAR是SQL的一些常量

帶參數預存程序
$db->executeProcedur($pName,$parName,$sqlTyle);
無參數預存程序
$db->executeProcedurNoPar($pName);

select * from t2 where t2.id in(select max(t2.id) from t1 join t2 on t1.id = t2.pid group by t1.id);
取每個分類的最新一條資料。此處做個記錄。
t1為類別表,t2為主表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.