PHP Surface Image Object Database operation class

Source: Internet
Author: User
<?php
//*******************************************************************
A database operation class is constructed here to encapsulate all database operations
can be extended to facilitate the use of background management programs
Class MySQLdb
{
var $host;
var $user;
var $passwd;
var $database;
var $conn;

Initialize a variable with a constructor implementation
Connecting to database operations at the same time
function MySQLdb ($host, $user, $password, $database)
{
$this->host = $host;
$this->user = $user;
$this->passwd = $password;
$this->database = $database;
$this->conn=mysql_connect ($this->host, $this->user, $this->passwd) or
Die ("Could not connect to $this->host");
mysql_select_db ($this->database, $this->conn) or
Die ("Could not switch to database $this->database");
}

This function is used to close the database connection
function Close ()
{
Mysql_close ($this->conn);
}

This function implements database query operation
function Query ($QUERYSTR)
{
$res =mysql_query ($queryStr, $this->conn) or
Die ("Could not query database");
return $res;
}

This function returns the recordset
function GetRows ($res)
{
$rowno = 0;
$rowno = mysql_num_rows ($res);
if ($rowno >0)
{
for ($row =0; $row < $rowno; $row + +)
{
$rows [$row]=mysql_fetch_array ($res);
Originally mysql_fetch_row, but cannot be extracted as an array, only indexed
This makes it easier to use indexes and names.
}
return $rows;
}
}

This function retrieves the number of database records
function Getrowsnum ($res)
{
$rowno = 0;
$rowno = mysql_num_rows ($res);
return $rowno;
}

This function returns the number of database table fields
function Getfieldsnum ($res)
{
$fieldno = 0;
$fieldno = Mysql_num_fields ($res);
return $fieldno;
}

This function returns the database table field name set
function GetFields ($res)
{
$FNO = $this->getfieldsnum ($res);
if ($fno >0)
{
for ($i =0; $i < $fno; $i + +)
{
$fs [$i]=mysql_field_name ($res, $i);//Take the name of the first field
}
return $FS;
}
}

}


Require the file directly when used, and then instantiate:

$SqlDB = new MySQLdb ("localhost", "root", "root", "TestDB");

$sql = "SELECT * from TableX ...";

$result = $SqlDB->query ($sql);//Query

$rs = $SqlDB->getrows ($result);//Get Recordset

$num = $SqlDB->getrowsnum ($result)//number of records obtained

... The rest of the operation is to loop the value,

for ($i =0; $i < $num; $i + +) {
Echo ($rs [$i] [field name]);
}

...

Finally, do not forget to close the data link oh $sqldb->close (); Of course, this can not, PHP will automatically log off! But this can develop a good habit, it is best to add!

... Others by analogy ... Do not understand can ask questions!

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.