Copy Code code as follows:
<?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 ' 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 ();
}
}
?>
Next Call
Copy Code code as follows:
function __autoload ($MssqlUtil)
{
Require $MssqlUtil. ' PHP ';
}
$db = new Mssqlutil ($config [' Host '], $config [' User '], $config [' Keys '], $config [' base ']);
The main next is a stored procedure call with parameters
Copy Code code as follows:
$pName Stored Procedure Name
$parName parameter, the parameter form is very important, is the array type, the corresponding relation is
Array (' @a ' => ' a ') @a is the parameter in the stored procedure, a is the value to be passed
$sqlTyle is the data type of the stored procedure parameter, is an array form, and also important
Array (Sqlchar,sqlvarchar), be careful not to add single quotes, and so on, because Sqlvarchar are some of the constants of SQL
Stored Procedures with parameters
$db->executeprocedur ($pName, $parName, $sqlTyle);
No parameter stored procedure
$db->executeprocedurnopar ($pName);
SELECT * from T2 where t2.id to (select Max (t2.id) from T1 join T2 to T1.id = T2.pid Group by t1.id);
Take the latest data for each category. Make a note here.
T1 for category table, T2 primary table