Ezsql is a very useful PHP database operation class. The famous open source blog WordPress database operation on the use of the Ezsql MySQL part. The database operations class supports virtually all major databases, such as Php-pdo, MySQL, Oracle, Interbase/firebird, PostgreSQL, SQLite, and Ms-sql. Ezsql has a strong debugging capability to quickly view the execution of SQL code. With Ezsql, we can save development time, simplify code, and improve operational efficiency.
The advantage of Ezsql is needless to say, it is small, fast, simple, easy to use, and open source. There is security, you have not thought of the details of it for you to consider. All you have to do is include the relevant PHP file at the beginning of your script, and then you can use a better set of ezsql functions instead of the standard PHP database operations functions.
Here are some of the main functions in Ezsql:
$DB->get_results-Reads the dataset from the database.
$DB->get_row--Reads a row of data from the database.
$DB->get_col-Reads a specified set of data from the database.
$DB->get_var-Reads a value from the dataset in the database.
$db->query-executes an SQL statement.
$db->debug--Prints the last executed SQL statement and the results returned by it.
$db->vardump--the structure and content of the printed variable.
$db->select--Select a new database.
$db->get_col_info--Gets the information for the column.
$db->hide_errors--Hide errors.
$db->show_errors--Displays an error.
Ezsql is easy to use, first download the Ezsql source code, and then put the ez_sql_core.php file and ez_sql_mysql.php file (here, for example, MySQL) in the same directory as your script file, Then add the following code to the front of your script file so that you can use ezsql normally.
<?php
Core files that contain ezsql
Include_once "ez_sql_core.php";
Contains ezsql specific database files, here is a case of MySQL
Include_once "ez_sql_mysql.php";
Initializing database objects and establishing a database connection
$db = new ezSQL_mysql (' Db_user ', ' Db_password ', ' db_name ', ' db_host ');
?>
The following is an example of an application of some of the main functions in Ezsql, which are derived from the official Help documentation for EZSQL.
Example one:
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 two:
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 three:
Get one variable from the database and print it out.
$var = $db->get_var ("SELECT count (*) from users");
Echo $var;
Example four:
Insert into the database
$db->query ("INSERT into users (ID, name, email) VALUES (NULL, ' Justin ', ' jv@foo.com ')");
Example five:
Update the database
$db->query ("UPDATE users SET name = ' Justin ' WHERE id = 2)");
Example SIX:
Display last query and all associated results
$db->debug ();
Example Seven:
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 eight:
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 nine:
Same as above ' but quicker '
foreach ($db->get_col ("SELECT name,email from Users", 0) as $name) {
Echo $name;
}
Example Ten:
Map out of the full schema, 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 ();