EzSQLPHP database operation Class Library

Source: Internet
Author: User
Tags php database
The database operation class used by WordPress is ezSQL. I have been using ezSQL for many years. I especially like its several class methods, which can effectively improve the code conciseness. EzSQL:
Download: ezSQL

The new version is 2.05, with many support added, including CodeIgniter, MSSQL, and PDO.
I wrote CodeIgniter once, but only MySQL is supported.

See examples
In fact, there is no difficulty, just look at the source code directly, mainly because the program design idea is very good.

Example 1
----------------------------------------------------

// Select multiple records from the database and print them out ..
$ Users = $ db-> get_results ("SELECT name, email FROM users ");
Foreach ($ users as $ user ){
// Access data using object syntax
Echo $ user-> name;
Echo $ user-> email;
}
Example 2
----------------------------------------------------

// Get one row from the database and print it out ..
$ User = $ db-> get_row ("SELECT name, email FROM users WHERE id = 2 ");
Echo $ user-> name;
Echo $ user-> email;
Example 3
----------------------------------------------------

// Get one variable from the database and print it out ..
$ Var = $ db-> get_var ("SELECT count (*) FROM users ");
Echo $ var;
Example 4
----------------------------------------------------

// Insert into the database
$ Db-> query ("insert into users (id, name, email) VALUES (NULL, 'Justin ', 'JV @ foo.com ')");
Example 5
----------------------------------------------------

// Update the database
$ Db-> query ("UPDATE users SET name = 'Justin 'WHERE id = 2 )");
Example 6
----------------------------------------------------

// Display last query and all associated results
$ Db-> debug ();
Example 7
----------------------------------------------------

// Display the structure and contents of any result (s) .. or any variable
$ Results = $ db-> get_results ("SELECT name, email FROM users ");
$ Db-> vardump ($ results );
Example 8
----------------------------------------------------

// Get 'one column' (based on column index) and print it out ..
$ Names = $ db-> get_col ("SELECT name, email FROM users", 0)
Foreach ($ names as $ name ){
Echo $ name;
}
Example 9
----------------------------------------------------

// Same as above 'but quicker'
Foreach ($ db-> get_col ("SELECT name, email FROM users", 0) as $ name ){
Echo $ name;
}
Example 10
----------------------------------------------------

// Map out the full schema of any given database and print it out ..
$ Db-> select ("my_database ");
Foreach ($ db-> get_col ("show tables", 0) as $ table_name ){
$ Db-> debug ();
$ Db-> get_results ("DESC $ table_name ");
}
$ Db-> debug ();

EZSQL class introduction:

Ezsql is a small and fast database operation class, you can easily use PHP to operate various databases (MySQL, oracle8/9, interbase, FireBird, PostgreSQL, MS-SQL, sqlite, and sqlite C ++ ).
At the beginning of your script, you must include a php file. Then, you can use a smaller and easier ezsql function to replace the standard PHP database function.
It automatically caches the query results and provides a series of simple function operations and extensions without causing additional server overhead.
It has excellent debugging functions, allowing you to quickly judge the execution process of SQL statements.
The result returned by the ezsql function is an object, an associated array, or a numeric array.
It can greatly shorten the development time, and in most cases, it will simplify your code, make it run faster, and it is easy to debug and optimize your database query statements.
This is a small category and does not add much overhead to your website.

Class has the following methods:
-$ Db-> get_results-read the dataset from the database (or the previously cached dataset)
-$ Db-> get_row-read a piece of data from the database (or previously cached data)
-$ Db-> get_col-read a specified column from the database (or the previously cached dataset)
-$ Db-> get_var-read a value from the database data set (the data cached before or)
-$ Db-> query-execute an SQL statement (if there is data, it will be cached)
-$ Db-> debug-print the final executed SQL statement and returned results (if any)
-$ Db-> vardump-print the structure and content of the variable
-$ Db-> select-select a new database
-$ Db-> get_col_info-get column information
-$ Db-> donation-donate money to the Author
-$ Db-> escape-format the string inserted into the database, for example, mysql_escape_string (stripslashes ($ str ))
-$ Db-> flush-clear cache
-$ Db-> get_cache-in exchange for cache
-$ Db-> hide_errors-hide error
-$ Db-> register_error-registration error
-$ Db-> show_errors-Display error
-$ Db-> store_cache-store to cache
-$ Db-> sysdate-obtain the system time
-$ Db = new db-create a new db object.

Wordpress modified ezsql and made it only applicable to mysql.

Some of the modified wordpress class operations are called functions as follows:

Function query ($ query)
This function is the most basic function of WPDB. $ query is an SQL statement and is submitted to the database for query. The results are divided into two situations:
1. if it is "insert | delete | update | replace", the number of affected rows is returned. in the case of "insert | replace", use $ this-> insert_id to record the newly inserted ID.
2. for "select", use $ this-> last_result to write down the query result set and return the number of queried records.

Function escape ($ string)
Use a backslash to reference a string, that is, use magic quotes.

Function insert ($ table, $ data)
This is the insert record function. The first parameter is the field array of the table, and the second parameter is the data array. 1 is returned for data insertion; otherwise, 0 is returned.

Function update ($ table, $ data, $ where)
This is the record update function. The first parameter is the field array of the table, the second is the data array, and the third is the condition array, which is an nane array. Updated to 1; otherwise, it is 0.

Function get_var ($ query = null, $ x = 0, $ y = 0)
If $ query is not empty, run the query first, and then return the value of Row Y in column X.

Function get_row ($ query = null, $ output = OBJECT, $ y = 0)
Returns a row. $ outpu specifies the returned type. it can be ARRAY_A, ARRAY_N, or OBJECT. $ Y specifies the row number.

Function get_col ($ query = null, $ x = 0)
Returns a column. $ x specifies the column number.

Function get_results ($ query = null, $ output = OBJECT)
Returns the query result set. ARRAY_A, ARRAY_N, or OBJECT can be returned.

Function get_col_info ($ info_type = 'name', $ col_offset =-1)
Return field information.

There are other functions, which are not detailed here. There are also two global variables, SAVEQUERIES and WP_DEBUG. The first one is that you can save the query executed on the access page to the $ this-> queries array, for later debugging, WP_DEBUG allows you to output errors. These two are not enabled by default. you can enable them in wp_config.php during the test.

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.