Common database operation in secondary development of destoon
- 2021-07-02 23:47:38
- OfStack
destoon After initializing the system, the system will automatically connect to the database and save the database operation object in $db. Please refer to the include/db_mysql. class. php function prototype for database operation methods, and the following examples of common database operations.
1. Execute the SQL statement
$db->query("INSERT INTO `{$DT_PRE}table` (`xxx`) VALUES ('yyy')");
$db->query("UPDATE `{$DT_PRE}table` SET `xxx`='yyy' WHERE `zzz`=1");
$db->query("DELETE FROM `{$DT_PRE}table` WHERE `zzz`=1");
2. Read multiple pieces of information
$A = array();
$result = $db->query("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy' ORDER BY `zzz` DESC LIMIT 0,10");
while($r = $db->fetch_array($result)) {
$A[] = $r;
}
print_r($A);
3. Read a single message
$A = $db->get_one("SELECT * FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
print_r($A);
4. Calculate the total
$A = $db->get_one("SELECT COUNT(*) AS num FROM `{$DT_PRE}table` WHERE `xxx`='yyy'");
echo $A['num'];
The table prefix of the system can use the variable $DT_PRE (1 is generally used in statements) or $db-
>
pre (1 is generally used in functions).
If you use a database operation in a function, you need to do global $db first;