A PHP class that operates MySQL

Source: Internet
Author: User
Tags array commit connect mysql php class printf

<?php
/*
* This class was originally part of the phplib and I made some changes.
*/
Class Mydb_sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";

var $Link _id = 0;
var $Query _id = 0;
var $Record = array ();
var $Row;

var $Errno = 0;
var $Error = "";

var $Auto _free = 0; # # Set this to 1 for automatic mysql_free_result ()
var $Auto _commit = 0; # # Set this to 1 to automatically commit results

var $debugmode = 0;

function Connect () {
if (0 = = $this->link_id) {
$this->link_id=mysql_pconnect ($this->host, $this->user, $this->password);
if (! $this->link_id) {
$this->halt ("Link-id = = False, Pconnect failed");
}
if (!mysql_query (sprintf ("Use%s", $this->database), $this->link_id)) {
$this->halt ("Cannot use database" $this->database);
}
}
}

function query ($Query _string) {
$this->connect ();

if ($this->debugmode)
printf ("Debug:query =%s<br>\n", $Query _string);

$this->query_id = mysql_query ($Query _string, $this->link_id);
$this->row = 0;
$this->errno = Mysql_errno ();
$this->error = Mysql_error ();
if (! $this->query_id) {
$this->halt ("Invalid SQL:". $Query _string);
}

return $this->query_id;
}

function Next_record () {
$this->record = mysql_fetch_array ($this->query_id);
$this->row + 1;
$this->errno = Mysql_errno ();
$this->error = Mysql_error ();

$stat = Is_array ($this->record);
if (! $stat && $this->auto_free) {
Mysql_free_result ($this->query_id);
$this->query_id = 0;
}
return $stat;
}

function Seek ($pos) {
$status = Mysql_data_seek ($this->query_id, $pos);
if ($status)
$this->row = $pos;
Return
}

function metadata ($table) {
$count = 0;
$id = 0;
$res = Array ();

$this->connect ();
$id = @mysql_list_fields ($this->database, $table);
if ($id < 0) {
$this->errno = Mysql_errno ();
$this->error = Mysql_error ();
$this->halt ("Metadata query failed.");
}
$count = Mysql_num_fields ($id);

for ($i =0; $i < $count; $i + +) {
$res [$i] ["table"] = mysql_field_table ($id, $i);
$res [$i] ["name"] = Mysql_field_name ($id, $i);
$res [$i] ["type"] = Mysql_field_type ($id, $i);
$res [$i] ["len"] = Mysql_field_len ($id, $i);
$res [$i] ["flags"] = Mysql_field_flags ($id, $i);
$res ["Meta"] [$res [$i] ["name"]] = $i;
$res ["Num_fields"]= $count;
}

Mysql_free_result ($id);
return $res;
}

function Affected_rows () {
Return Mysql_affected_rows ($this->link_id);
}

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

function Num_fields () {
Return Mysql_num_fields ($this->query_id);
}

function NF () {
return $this->num_rows ();
}

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

function f ($Name) {
return $this->record[$Name];
}

function P ($Name) {
Print $this->record[$Name];
}

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

Class SQL extends Mydb_sql {
var $Host = "localhost";
var $Database = "";
var $User = "";
var $Password = "";

function Free_result () {
Return @mysql_free_result ($this->query_id);
}

function rollback () {
return 1;
}

function commit () {
return 1;
}

function Autocommit ($onezero) {
return 1;
}

function insert_id ($col = "", $tbl = "", $qual = "") {
Return mysql_insert_id ($this->query_id);
}
}
?>



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.