PHP implementation of SQLite database connection class _php skills

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

This article describes the PHP implementation of the SQLite database connection classes. Share to everyone for your reference. The implementation methods are as follows:

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

Copy Code code 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 number
* @param string $ database server password
* @param string $ database name
* @param bool $ to maintain continuous connection, 1 for continuous connection, 0 for non-continuous 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 null, 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 Quantity

*/
function GetList ($table, $wheres = "1=1", $colums = ' * ', $limits = ' 3000 ', $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, $dbname);
Else
$query = $this-> query ("Delete from". $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 with only one record
*
* @param string $ SQL statement
* @param string $ default is null, 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 one row from the result set as an associative array
*
* @param resource $ database Query result resource
* @param string $ define return type
* @return Array
*/
function Fetch_array ($query, $result _type = Sqlite_assoc) {
Return Sqlite_fetch_array ($query, $result _type);
}
/**
* 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 standby, generally not.
*/
function result ($query, $row) {
Return @sqlite_fetch_all ($query, SQLITE_ASSOC);
}
/**
* SQLite no corresponding function
*/
function Free_result ($query) {
return;
}
/**
* Get the ID generated by the previous insert operation
*
* @return int
*/
function insert_id () {
Return Sqlite_last_insert_rowid ($this-> Connid);
}
/**
*
* @return Array only get digital 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 <br> sqliteerror:". $this-> error (). "<br> Sqliteerrno:". $this-> errno (). "<br> message: $message");
}

I hope this article will help you with your PHP database program design.

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.