PHP program to connect to a common class of SQL Server databases

Source: Internet
Author: User
Tags insert connect mssql printf query
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

$servername ");
Database, $this->link_id "> $db = @mssql_select_db ($this->database, $this->link_id);
if (! $this->link_id) {
$this->halt ("Link-id = = False, Mssql_connect failed");
}
}
}

/****************************************
* 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 ();

$this->query_id = mssql_query ($Query _string);
$this->row = 0;
if (! $this->query_id) {
$msg =mssql_get_last_message ();

if ($msg ==null | | $msg = "")
{
$this->affnum=1;
return 1;
}

if (Strtolower (substr ($Query _string,0,6))!= "select") {
$this->affnum=1;
return 1;
}

$this->errno = 1;
$this->error = "General Error (the MSSQL interface cannot return detailed Error messages) (". $msg. ").";
$this->halt ("Invalid SQL:". $Query _string);
}
return $this->query_id;
}

/*******************************************************
* 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 following are examples of usage:

?
Constructing a new DB class
$DBConn =new DB;

Write SQL query statement
$SQLSTR = "SELECT * from Test";
$DBConn->query ($SQLSTR);

The results of the circular output query
while ($Row = $DBConn->nextrecord ()) {
echo $Row [TestID];
}

To close a database connection
$DBConn->close ();
?>

Related Article

Contact Us

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.

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.