Pseudo-MSSQL PDO for PHP.
Why to use it?
- It's safe against SQL Injection;
- It's easier to use than the original PDO;
- You don't need to use mssql_fetch_array or mssql_free_result, since its automatic;
- More organization in your PHP scripts.
Code:
PHP Code:
<?php
class MSSQL
{
public $query = null;
public $fetch = null;
protected $fetca;
protected $mfetc;
public function __construct ($fetch = false, $multi = false)
{
$fetch ? $this->fetca = true : $this->fetca = false;
$multi ? $this->mfetc = true : $this->mfetc = false;
}
private function escape($arg)
{
foreach ($arg as $value)
{
is_numeric($value) ? $return[] = $value : $return[] = "N'".str_replace("'", "''", $value)."'";
}
return $return;
}
public function query()
{
if (!func_num_args()) return false;
$arg = func_get_args();
$query = $arg[0];
unset($arg[0]);
if (count($arg)) $query = vsprintf($query, $this->escape($arg));
$this->query = mssql_query($query);
if ($this->fetca):
if ($this->mfetc):
while ($fetch = mssql_fetch_array($this->query))
$this->fetch[] = $fetch;
else:
$this->fetch[] = $fetch;
endif;
endif;
mssql_free_result($this->query);
return true;
}
public function m_fetch()
{
$return = null;
$i = 0;
while(isset($this->fetch[$i])):
$return[] = $this->fetch[$i];
$i++;
endwhile;
return $return;
}
}
?>
Examples:
PHP Code:
require("mssql.php");
mssql_pconnect(MSSQL_HOST, MSSQL_USER, MSSQL_PASS);
#A Simple query without args but with multiple fetch results:
function GetBannedAccs()
{
$query = "SELECT [account] FROM ACCOUNT_DBF.dbo.ACCOUNT_TBL_DETAIL where BlockTime=99999999";
$mssql = new mssql(true, true);
$mssql->query($query) ? $return = $mssql->m_fetch() : $return = false;
return $return;
}
#A Simple query without fetch results and with 1 arg:
function UnBanByAcc($account)
{
$query = "UPDATE ACCOUNT_DBF.dbo.ACCOUNT_TBL_DETAIL SET BlockTime=20041111 WHERE account=%s";
$mssql = new mssql();
return $mssql->query($query, $account);
}
#Query with 1 arg and only 1 fetch result:
function GetAccount($charname)
{
$return = null;
$query = "SELECT [account] FROM [CHARACTER_01_DBF].[dbo].[CHARACTER_TBL] where m_szName = %s";
$mssql = new mssql(true);
$mssql->query($query, $charname) ? $return = $mssql->fetch[0] : $return = false;
return $return;
}