php的mssql数据库连接类实例
程序员文章站
2022-06-05 21:31:25
本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:class db_sql {
v...
本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
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 these: '$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>: %s (%s)<br> ",
$this->errno,
$this->error);
die("session halted.");
}
}
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 these: '$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>: %s (%s)<br> ",
$this->errno,
$this->error);
die("session halted.");
}
}
希望本文所述对大家的php程序设计有所帮助。