PHP implementation of the SQLite database connection class, SQLite database connection _php Tutorial

Source: Internet
Author: User
Tags mysql tutorial pconnect php database sqlite database

PHP implementation of the SQLite database connection class, SQLite database connection


This paper describes the database connection class of SQLite implemented by PHP. Share to everyone for your reference. The implementation method is as follows:

The SQLite database connection class is the use of PHP and SQLite connection operation, the code is as follows:

Copy CodeThe code is as follows: */
Lass Db_class {
var $conn =null;
var $querynum = 0;
/**
* Database connection, return database connection identifier
*
* @param string $ database server host
* @param string $ database server account
* @param string $ database server password
* @param string $ database name
* @param bool $ to maintain continuous connection, 1 for persistent connection, 0 for non-persistent connection
* @return Link_identifier $dbuser, $DBPW, $dbname,
*/
Function Connect ($dbhost, $pconnect = 0) {
$error = ";
$func = $pconnect = = 1? ' Sqlite_popen ': ' Sqlite_open ';
if (! $this, conn = $func ($dbhost, 0666, $error)) {
$this-Halt ($error);
}
return $this, Conn;
}
/**
* Execute SQL statement
*
* @param string $ SQL statement
* @param string $ default is empty and the optional value is cache unbuffered
* @param int $ cache life cycle in seconds
* @return Resource
*/
function query ($sql, $type = ', $expires = 3600, $dbname = ') {
$error = ";
$func = $type = = = ' unbuffered '? ' Sqlite_unbuffered_query ': ' Sqlite_query ';
if (Preg_match ("/^s*select/i", $sql)) {
$query = $func ($this, Conn, $sql, SQLITE_ASSOC, $error);
} else {
$query = Sqlite_exec ($this, Conn, $sql, $error);
}
if ($error) {
$this-Halt ($error, $sql);
}
$this, querynum++;
return $query;
}
/*
* @param string $ table name
* @param string $ where condition
* @param string $ colum Name

* @param string $ limit number

*/
function GetList ($table, $wheres = "1=1", $colums = ' * ', $limits = ' + ', $orderbys = "id desc") {
$query = $this Query ("select". $colums. "from". $table. "where". $wheres. "ORDER BY". $orderbys. "Limit". $limits, $ Type, $expires, $dbname);
while ($rs = $this, Fetch_array ($query)) {
$datas []= $rs;
}
Print_r ("select". $colums. "from". $table. "where". $wheres. "Limit". $limits);
Print_r ($rs);d ie ();
$this-Free_result ($query);
return $datas;
}
function Add_one ($table, $colums, $data) {
Die ("insert into". $table. " (". $colums.") VALUES (". $data.)");
$query = $this query ("INSERT into". $table. " (". $colums.") VALUES (". $data.") ", $type, $expires, $dbname);
return $this->insert_id ();
return $query;
}
Function Delist ($table, $idarray, $wheres = "No") {
if ($wheres = = ' no ')
$query = $this query ("Delete from". $table. "Where ID in (". $idarray. ")", $type, $expires, and $dbname);
Else
$query = Query ("Delete from"), $this. $table. "where". $wheres, $type, $expires, $dbname);
return $query;
}
function Updatelist ($table, $updatedata, $idarray) {
$query = $this query ("Update". $table. "Set". $updatedata. "Where ID in (". $idarray. ")", $type, $expires, $dbname);
return $query;
}
Update max_vote set maxtitle= ' $title ', maxban= ' $ban ',
/**
* Execute SQL statement, get only one record
*
* @param string $ SQL statement
* @param string $ default is empty and the optional value is cache unbuffered
* @param int $ cache life cycle in seconds
* @return Array
*/
function Get_one ($sql, $type = ", $expires = 3600, $dbname =") {
$query = $this, query ($sql, $type, $expires, $dbname);
$rs = $this-Fetch_array ($query);
$this-Free_result ($query);
return $rs;
}
/**
* Get a row from the result set as an associative array
*
* @param resource $ database Query result resource
* @param string $ defines the return type
* @return Array
*/
function Fetch_array ($query, $result _type = Sqlite_assoc) {
Return Sqlite_fetch_array ($query, $result _type);
}
/**
* Gets the number of record rows affected by the previous SQLite operation
*
* @return int
*/
function Affected_rows () {
Return Sqlite_changes ($this-conn);
}
/**
* Get the number of rows in the result set
*
* @return int
*/
function Num_rows ($query) {
Return Sqlite_num_rows ($query);
}
/**
* Returns the number of fields in the result set
*
* @return int
*/
function Num_fields ($query) {
Return Sqlite_num_fields ($query);
}
/**
*
* @return array backup, generally not used.
*/
function result ($query, $row) {
Return @sqlite_fetch_all ($query, SQLITE_ASSOC);
}
/**
* SQLite has no corresponding function
*/
function Free_result ($query) {
return;
}
/**
* Get the ID generated by the insert operation in the previous step
*
* @return int
*/
function insert_id () {
Return Sqlite_last_insert_rowid ($this-Connid);
}
/**
*
* @return Array only gets the numeric index
*/
function Fetch_row ($query) {
Return Sqlite_fetch_array ($query, sqlite_num);
}
/**
*/
function Fetch_assoc ($query) {
return $this Fetch_array ($query, SQLITE_ASSOC);
}
/**
*
* @return String
*/
Function version () {
return Sqlite_libversion ();
}
function Close () {
Return Sqlite_close ($this-conn);
}
/**
*
* @return String
*/
Function error () {
Return sqlite_error_string ($this-errno);
}
/**
*
* @return int
*/
function errno () {
Return Sqlite_last_error ($this-conn);
}
/**
* Show MySQL Tutorial error message
*/
function Halt ($message = ', $sql = ') {
Exit ("Sqlitequery: $sql
Sqliteerror: ". Error () $this. "
Sqliteerrno: ". $this, errno (). "
Message: $message ");
}

I hope this article is helpful to the PHP database design.

http://www.bkjia.com/PHPjc/926870.html www.bkjia.com true http://www.bkjia.com/PHPjc/926870.html techarticle PHP Implementation of the SQLite database connection class, SQLite database connection In this paper, we describe the implementation of the SQLite database connection class in PHP. Share to everyone for your reference. The concrete implementation method is as follows: ...

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