PHP Surface Image Object Database operation class instance, php instance _php tutorial

Source: Internet
Author: User
Tags php example

PHP Surface Image Object Database operation class instance, PHP instance


This paper describes the PHP surface object database operation class. Share to everyone for your reference.

The specific implementation code is as follows:

Copy CodeThe code is as follows://Here constructs a database operation class, encapsulates all database operations
can be extended to facilitate the use of the hypervisor
Class MySQLdb
{
var $host;
var $user;
var $passwd;
var $database;
var $conn;

Implementing variable initialization with a constructor function
Connect 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 the database query operation
function Query ($QUERYSTR)
{
$res =mysql_query ($queryStr, $this->conn) or
Die ("Could not Query Database");
return $res;
}

The function returns the record set
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 in an array, only indexed
This makes it easier to use indexes and names.
}
return $rows;
}
}

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

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

This function returns the set of database table field names
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 you use it, and then instantiate it:

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

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

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

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

$num = $SqlDB->getrowsnum ($result);//Get the number of records

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

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

...
Finally, don't forget to close the data path connectionCopy CodeThe code is as follows: $SqlDB->close (); Of course, this sentence can not be, PHP will automatically log off! But this can form a good habit, it is best to add! Other analogies.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/920615.html www.bkjia.com true http://www.bkjia.com/PHPjc/920615.html techarticle PHP Surface Image Object Database operation class instance, php example this article describes the PHP surface object database operation class. Share to everyone for your reference. The specific implementation code is as follows: Copy code ...

  • 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.