PHP's MSSQL database connection class instance _php skill

Source: Internet
Author: User
Tags mssql

The example of this article describes the PHP MSSQL database connection Class example code, share for everyone to reference.

The specific implementation code is as follows:

Copy Code code as follows:
Class Db_sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Link _id = 0;
var $Query _id = 0;
var $Record = array ();
var $Row = 0;

var $Errno = 0;
var $Error = "";
var $Auto _free = 0; # # Set this to 1 to automatically free results

function Db_sql ($query = "") {
$this->query ($query);
}
function Connect () {
if (0 = = $this->link_id) {
$this->link_id=mssql_connect ($this->host, $this->user, $this->password);
if (! $this->link_id)
$this->halt ("Link-id = = False, Mssql_pconnect failed");
Else
@mssql_select_db ($this->database, $this->link_id);
}
}
function Free_result () {
Mssql_free_result ($this->query_id);
$this->query_id = 0;
}

function query ($Query _string)
{

/* No empty queries, please, since PHP4 chokes on them. */
if ($Query _string = "")
/* The empty query string is passed on from the constructor,
* When calling the class without a query, e.g. in situations
* like: ' $db = new Db_sql_subclass; '
*/
return 0;
if (! $this->link_id)
$this->connect ();

# printf ("<br>debug:query =%s<br>", $Query _string);

$this->query_id = mssql_query ($Query _string, $this->link_id);
$this->row = 0;
if (! $this->query_id) {
$this->errno = 1;
$this->error = "General Error (the MSSQL interface cannot return detailed Error messages)."
$this->halt ("Invalid SQL:". $Query _string);
}
return $this->query_id;
}

function Next_record () {

if ($this->record = Mssql_fetch_row ($this->query_id)) {
Add to Record[<key>]
$count = Mssql_num_fields ($this->query_id);
for ($i =0; $i < $count; $i + +) {
$fieldinfo = Mssql_fetch_field ($this->query_id, $i);
$this->record[strtolower ($fieldinfo->name)] = $this->record[$i];
}
$this->row + 1;
$stat = 1;
} else {
if ($this->auto_free) {
$this->free_result ();
}
$stat = 0;
}
return $stat;
}

function Seek ($pos) {
Mssql_data_seek ($this->query_id, $pos);
$this->row = $pos;
}
function metadata ($table) {
$count = 0;
$id = 0;
$res = Array ();
$this->connect ();
$id = Mssql_query ("SELECT * from $table", $this->link_id);
if (! $id) {
$this->errno = 1;
$this->error = "General Error (the MSSQL interface cannot return detailed Error messages)."
$this->halt ("Metadata query failed.");
}
$count = Mssql_num_fields ($id);

for ($i =0; $i < $count; $i + +) {
$info = Mssql_fetch_field ($id, $i);
$res [$i] ["table"] = $table;
$res [$i] ["name"] = $info ["name"];
$res [$i] ["len"] = $info ["Max_length"];
$res [$i] ["flags"] = $info ["Numeric"];
}
$this->free_result ();
return $res;
}

function Affected_rows () {
Not a supported function in PHP3/4. Chris Johnson, 16may2001.
Return Mssql_affected_rows ($this->query_id);
$rsRows = Mssql_query ("SELECT @ @rowcount as rows", $this->link_id);
if ($rsRows) {
Return Mssql_result ($rsRows, 0, "rows");
}
}

function Num_rows () {
Return Mssql_num_rows ($this->query_id);
}

function Num_fields () {
Return Mssql_num_fields ($this->query_id);
}
function NF () {
return $this->num_rows ();
}

function np () {
Print $this->num_rows ();
}

function f ($Field _name) {
return $this->record[strtolower ($Field _name)];
}

function P ($Field _name) {
Print $this->f ($Field _name);
}

function Halt ($msg) {
printf ("</td></tr></table><b>database error:</b>%s<br>", $msg);
printf ("<b>mssql Error</b&gt:%s (%s) <br>",
$this->errno,
$this->error);
Die ("session halted.");
}
}

I hope this article will help you with your PHP program design.

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.