In DEDECMS, establishing a database connection is the first step before performing any data operations. Developers must connect to the database to query, insert, update, or delete data. Here is a basic database connection example:
<?php
require_once(dirname(__FILE__)."/include/common.inc.php");
$mysql_servername = $GLOBALS['cfg_dbhost']; // Database host
$mysql_username = $GLOBALS['cfg_dbuser']; // Database username
$mysql_password = $GLOBALS['cfg_dbpwd']; // Database password
$mysql_dbname = $GLOBALS['cfg_dbname']; // Database name
$link = mysql_connect($mysql_servername, $mysql_username, $mysql_password);
mysql_select_db($mysql_dbname, $link);
mysql_query("SET NAMES utf8");
?>
Querying data is one of the most common operations in DEDECMS. By executing SQL statements, you can retrieve the information you need from the database. Here is an example of querying data:
<?php
$sql = "SELECT * FROM `dede_archives` WHERE `typeid` = 1";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo $row['title'].'<br>';
}
?>
Inserting data is a fundamental operation in content management systems. Using the INSERT statement, you can add new content to the database. Here is a simple example:
<?php $title = 'Test Title'; $content = 'Test Content'; $sql = "INSERT INTO `dede_archives` (`typeid`, `title`, `body`) VALUES (1, '$title', '$content')"; mysql_query($sql); ?>
Updating existing data is a common task in DEDECMS. The UPDATE statement allows you to modify records in the database. Here is an example:
<?php $title = 'Updated Title'; $content = 'Updated Content'; $sql = "UPDATE `dede_archives` SET `title` = '$title', `body` = '$content' WHERE `aid` = 1"; mysql_query($sql); ?>
Deleting data is an essential operation for database management. The DELETE statement removes unwanted records. Here is an example:
<?php $sql = "DELETE FROM `dede_archives` WHERE `aid` = 1"; mysql_query($sql); ?>
These examples demonstrate the basic flow of database operations in DEDECMS, including connection, querying, insertion, update, and deletion. Developers can flexibly use these functions to efficiently manage website data according to project requirements.