Ecshop does not use some open-source database operation classes, such as ADODB or pear, but encapsulates its own implementation. The benefit of this is a very lightweight implementation, which greatly reduces the file size of the distribution package. In addition, when the site needs to do memcached cache, it can also be very convenient to implement. Of course, the consequence of this is that the choice of database is very narrow, unable to implement other non-MySQL database.
The Ecshop data manipulation class file is includes/cls_mysql.php, and the class name is Cls_mysql.
This class mainly provides some of the more useful methods below:
GetAll ($sql) and getallcached ($sql, $cached = ' Filefirst '): Gets all records.
GetRow ($sql, $limited = False) and getrowcached ($sql, $cached = ' Filefirst '): Gets a single-line record.
Getcol ($sqlse) and getcolcached ($sql, $cached = ' Filefirst '): Gets all values for a field.
GetOne ($sql, $limited = False) and getonecached ($sql, $cached = ' Filefirst '): Gets a single value.
Query ($SQL): Executes a database query.
Autoexecute ($table, $field _values, $mode = ' INSERT ', $where = '): Database table operation.
Now let's illustrate how these methods are used in an instance way. First, add the file test_mysql.php in the Ecshop/admin directory, the file content is as follows:
Define (' In_ecs ', true);
Define (' Ec_charset ', ' utf-8 ');
Define (' Root_path ', ' D:/program files/zend/apache2/htdocs/ecshop/');
Define (' Data_dir ', ' DATA ');
$db _host = "localhost:3306";
$db _name = "Ecshop";
$db _user = "root";
$db _pass = "";
Require ('.. /includes/cls_mysql.php ');
$db = new Cls_mysql ($db _host, $db _user, $db _pass, $db _name);
Get All records
The GetAll method is used to get all the records that meet the criteria from the database. Getallcached is its cached version, the cache key is the second parameter of the method, and if the cache is valid, return the cached result directly, or re-execute the database query.
Add the following code to the end of test_mysql.php:
Test_getall ();
function Test_getall ()
{
global$db;
$sql = "Select user_id, user_name, email from ecs_admin_user";
$result = $db->getall ($sql);
Print_r ($result);
}
Get a single row of records
The GetRow method is used to get a single row of records that satisfy a condition from the database, or the first record. Getrowcached is its cached version, the cache key is the second parameter of the method, and if the cache is valid, return the cached result directly, or re-execute the database query.
Add the following code to the end of test_mysql.php:
Test_getrow ();
function Test_getrow ()
{
global$db;
$sql = "Select user_id, user_name, email from ecs_admin_user LIMIT 1";
$result = $db->getrow ($sql);
Print_r ($result);
}
Get all values for a field
The Getcol method is used to get all the values of a field that satisfies a condition from the database. Getcolcached is its cached version, the cache key is the second parameter of the method, and if the cache is valid, return the cached result directly, or re-execute the database query.
Add the following code to the end of test_mysql.php:
Test_getcol ();
function Test_getcol ()
{
global$db;
$sql = "Select email from ecs_admin_user";
$result = $db->getcol ($sql);
Print_r ($result);
}
Get a single value
The GetOne method is used to get a single value from the database that satisfies the condition. Getonecached is its cached version, the cache key is the second parameter of the method, and if the cache is valid, return the cached result directly, or re-execute the database query.
Add the following code to the end of test_mysql.php:
Test_getone ();
function Test_getone ()
{
global$db;
$sql = "Select email from ecs_admin_user WHERE user_id = 4";
$result = $db->getone ($sql);
Print_r ($result);
}
Execute database Query
The query method is used to perform database queries, such as Insert,update,delete.
Add the following code to the end of test_mysql.php:
Test_query ();
function Test_query ()
{
global$db;
$sql = "UPDATE ecs_admin_user SET todolist = ' You have a new email! ' WHERE user_id = 4 ";
$db->query ($sql);
$sql = "Select ToDoList from ecs_admin_user WHERE user_id = 4";
$result = $db->getone ($sql);
Print_r ($result);
}
database table Operations
The Autoexecute method is used to simplify insert and update of data tables.
Add the following code to the end of test_mysql.php:
Test_autoexecute ();
function Test_autoexecute ()
{
global$db;
$table = "Ecs_role";
$field _values = Array ("Role_name" = "General Manager Office", "role_describe" = "General Manager Office", "Action_list" and "All");
$db->autoexecute ($table, $field _values, "INSERT");
Executed Sql:insert into Ecs_role (Role_name, Action_list, Role_describe) VALUES (' General Manager Office ', ' all ', ' General Manager Office ') $role _id = $db->in SERT_ID (); The new record id:5$field_values = Array ("action_list" = "goods_manage");
$db->autoexecute ($table, $field _values, "UPDATE", "role_id = $role _id");
Executed sql:update ecs_role SET action_list = ' goods_manage ' WHERE role_id = 5$sql = "Select Action_list from Ecs_role WHERE role_id = $role _id ";
$result = $db->getone ($sql);
Print_r ($result);
}
$db->getall ($sql) returns all results in the query data table, in the form of a two-dimensional associative array. If the result is assigned to smarty it is very convenient to use the loop to refer to the template.
$db->getone ($sql) returns the first field of a query
Like what:
$sql = "SELECT count (*) from ecs_goods";
$count = $db->getone ($sql);
$count is the total number of product data
$db->getrow ($sql) returns a row of data in the database such as
$sql = "SELECT * from Ecs_goods";
$row = $db->getrow ($sql);
The $row is a one-dimensional associative array that can be obtained by $row[' Goods_name '] and so on.
Actually, the result here
$row _all = $db->getall ($sql);
$row = $db->getrow ($sql);
$row is actually equal to $row _all[0] Of course you can get other values through loops such as $row _all[1] ...
$db->getone one field at a line
$db->getrow a row of records
$db->getall All records