Many friends asked me how to use the PHP program to connect the SQL Server database generic class, in fact this is not difficult, the key is a little bit careful, a little patience, with passion to understand
?
Class DB {
var $Host = "192.168.0.199"; Hostname of our MySQL server
var $Database = "Test"; Logical database name on this server
var $User = "User"; Database User
var $Password = "Password"; Database user ' s password
var $Link _id = 0; Result of Mssql_connect ()
var $Query _id = 0; Result of most recent mssql_query ()
var $Row = 0; Current row number
var $Errno = 0; Error State of query
var $Error = "";
var $AffNum = 0;
/**************************************
* Print Error Method: Displays the error message for the page processing.
****************************************/
function Halt ($msg) {
printf ("</td></tr></table><b>database error:</b>%s<br>\n", $msg);
printf ("<b>mssql Error</b>:%s (%s) <br>\n",
$this->errno,
$this->error);
Die ("session halted.");
}
/**************************************
* Connect to the database and select the default database
**************************************/
function Connect () {
if (0 = = $this->link_id) {
$this->link_id=mssql_connect ($this->host, $this->user, $this->password) or Die ("couldn ' t connect to SQL Server on
/****************************************
* Close the database and close it if the database connection is already open
* Please use Close () after calling connect () and processing.
****************************************/
function Close () {
if (0!= $this->link_id) {
Mssql_close ();
}
}
/*************************************************
* Input SQL statements, there are select,update,insert,delete
* Including stored procedures can also be invoked by this method.
*************************************************/
function Query ($Query _string) {
$this->connect ();
/*******************************************************
* Move the query database pointer to the next record
*******************************************************/
function NextRecord () {
$this->record = Array ();
Mssql_next_result ($this->query_id);
$this->record=mssql_fetch_array ($this->query_id);
$result = $this->record;
if (!is_array ($result)) return $this->record;
foreach ($result as $key => $value) {
$keylower =strtolower ($key);
if ($keylower!= $key) $this->record[$keylower]= $value;
}
return $this->record;
}
/********************************************************
* Reposition a pointer to the query database
********************************************************/
function Seek ($pos) {
if ($pos <=0) return;
if (Eregi ("[0-9]", $pos)) Mssql_data_seek ($this->query_id, $pos);
}
/********************************************************
* Get the total number of rows from the query database
********************************************************/
function NumRows () {
if ($this->query_id) $num _rows=mssql_num_rows ($this->query_id);
else $num _rows= $this->affnum;
return $num _rows;
}
/*******************************************************
* Number of fields
*******************************************************/
function Numfields () {
Return count ($this->record)/2;
}
/*******************************
* The value of the field
*******************************/
function Fieldvalue ($Field _name) {
return $this->record[$Field _name];
}
/******************************
Number of rows affected by *update,insert,delete
******************************/
function AffectedRows () {
if ($this->query_id) return mssql_num_rows ($this->query_id);
else{
return $this->affnum;
}
}
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.