Clase de acceso a BD MS SQL Server


Muy sencillo de utilizar esta clase de resultado devuelto la consulta y el acceso a los campos por su nombre de base de datos de MSSQL. Probado en php v. 3.xy 4.x con MS SQL Server 6.5 y 7.0





CODIGO:

connection=$Conn; 
$this->sql=$Sql; 
$this->fn=array(); 
$this->rowset=array(); 
$this->Query(); 
} 

// Execute query 

function Query() { 
$this->Close(); 
$this->result = @mssql_query($this->sql,$this->connection); 
if (!$this->result) 
return(0); 
$this->rowcount = @mssql_num_rows($this->result); 
$this->fieldcount = @mssql_num_fields($this->result); 
for ($i=1;$i<=$this->fieldcount;$i++) { 
$f=@mssql_fetch_field($this->result,$i-1); 
// Fill fields names array 
$this->fn[$i]=$f->name; 
} 
$this->curpos=0; 
} 

//Move to first record 

function MoveFirst() { 
$this->curpos=0; 
} 

//Return current row elements and move to next record 

function MoveNext() { 
if (!$this->result) return(0); 
if ($this->curpos==$this->rowcount) return(0); 
for($i=1;$i<=$this->fieldcount;$i++) 
$this->rowset[$this->fn[$i]] = @mssql_result($this->result,$this->curpos,$this->fn[$i]); 
$this->curpos++; 
return($this->rowset); 
} 

//Return true if last record 

function Eof() { 
if ($this->curpos==$this->rowcount-1) 
return(1); 
return(0); 
} 

//Return true if first record 

function Bof() { 
if (!$this->curpos) 
return(1); 
return(0); 
} 

// Free result if exist 

function Close() { 
if ($this->result && $this->rowcount) 
mssql_free_result($this->result); 
$this->result=0; 
$this->fn=array(); 
$this->rowset=array(); 
$this->rowcount=0; 
$this->fieldcount=0; 
} 
} 
?>

EJEMPLO:

MoveNext()) 
print $Fields["id"].",".$Fields["name"]; 
$rs->Close(); 

$rs->sql="insert into mytable(id,name) values(1,'test')"; 
$rs->Query(); 
$rs->Close(); 

$rs->sql="select id from mytable"; 
$rs->Query(); 
$Fields=$rs->MoveNext(); 
while(!$rs->Eof()) { //Eof method example 
... 
$Fields=$rs->MoveNext(); 
} 
$rs->MoveFirst(); //Move to first record 
.... 
$rs->Close(); 
?>

0 comentarios:

Publicar un comentario en la entrada