Ezsql PHP database operation class library

Source: Internet
Author: User
Tags codeigniter

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. Let's look at it directly.Source codeYes, mainlyProgramThe design idea is 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 yourCodeTo 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.

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.