Sqlite database connection class implemented by PHP

Source: Internet
Author: User
Tags pconnect php database
This article mainly introduces the sqlite database connection class implemented by PHP, which involves the usage of SQL operations such as connection, addition, deletion, modification, and query for SQLite databases. it is of great practical value, for more information about the sqlite database connection class implemented by PHP, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:

The sqlite database connection class uses php to connect to sqlite. the code is as follows:

The code is as follows:

*/
Lass db_class {
Var $ conn = null;
Var $ querynum = 0;
/**
* Database connection. the database connection identifier is returned.
*
* @ Param string $ database server host
* @ Param string $ database server account
* @ Param string $ database server password
* @ Param string $ database name
* @ Param bool $ indicates whether to maintain a persistent connection. 1 indicates a persistent connection, and 0 indicates a 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 statements
*
* @ Param string $ SQL statement
* @ Param string $ the default value is null. the optional value is cache unbuffered.
* @ Param int $ cache lifecycle 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 = '000000', $ orderbys = "id desc "){
$ Query = $ this-> query ("select ". $ colums. "from ". $ table. "where ". $ wheres. "order ". $ 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); die ();
$ This-> free_result ($ query );
Return $ datas;
}
Function add_one ($ table, $ colums, $ data ){
// Die ("insert into". $ table. "(". $ colums. ") values (". $ data .")");
$ Query = $ this-> query ("insert ". $ 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 the SQL statement and get only one record
*
* @ Param string $ SQL statement
* @ Param string $ the default value is null. the optional value is cache unbuffered.
* @ Param int $ cache lifecycle 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 associated 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 );
}
/**
* Obtain the number of rows affected by the previous sqlite operation.
*
* @ Return int
*/
Function affected_rows (){
Return sqlite_changes ($ this-> conn );
}
/**
* Number of rows in the result set
*
* @ Return int
*/
Function num_rows ($ query ){
Return sqlite_num_rows ($ query );
}
/**
* Number of fields in the returned result set
*
* @ Return int
*/
Function num_fields ($ query ){
Return sqlite_num_fields ($ query );
}
/**
*
* @ Return array backup, generally not required.
*/
Function result ($ query, $ row ){
Return @ sqlite_fetch_all ($ query, sqlite_assoc );
}
/**
* Sqlite does not have a function.
*/
Function free_result ($ query ){
Return;
}
/**
* Obtain the id generated by the previous insert operation.
*
* @ Return int
*/
Function insert_id (){
Return sqlite_last_insert_rowid ($ this-> connid );
}
/**
*
* @ Return array only obtains 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 );
}
/**
* Display the mysql tutorial error message
*/
Function halt ($ message = '', $ SQL = ''){
Exit ("sqlitequery: $ SQL
Sqliteerror: ". $ this-> error ()."
Sqliteerrno: ". $ this-> errno ()."
Message: $ message ");
}

I hope this article will help you design PHP database programs.

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.