Introduction to writing extended SQL programs for Imperial CMS
Basic example:
Note: The following examples are based on placing PHP files in the system root directory.
Example 1: Connect to MYSQL program. (a.php)
##<?phprequire('e/class/connect.php'); //Introduce the database configuration file and Public function file require('e/class/db_sql.php'); //Introduce database operation file $link=db_connect(); //Connect to MYSQL $empire=new mysqlquery(); //Declare the database operation class
db_close();
| #Example 2: Program to operate MYSQL data. (b.php)
##<?phprequire('e/class/connect.php'); //Introduce the database configuration file and Public function filerequire('e/class/db_sql.php'); //Introduce database operation file $link=db_connect(); //Connect to MYSQL$empire=new mysqlquery(); //Declare the database operation class$empire->query("update {$dbtbpre}ecms_news set onclick=onclick 1"); //Add 1 to the number of clicks in the news table db_close(); ‐ ’ s ’ s ’ s ’ s ’ ‐ ‐ ‐ ‐ out to MYSQLBlow out out out out out out out out out out out out out out's's to ’ s ’ s ’ ‐ ‐ ‐‐‐‐‐‐‐
Example 3: Program to read MYSQL data. (c.php)
##<?phprequire('e/class/connect.php'); //Introduce the database configuration file and Public function file require('e/class/db_sql.php'); //Introduce database operation file $link=db_connect(); //Connect to MYSQL $empire=new mysqlquery(); //Declare the database operation class
$sql=$empire->query("select * from {$dbtbpre}ecms_news order by newstime limit 10"); //Query the latest 10 records of the news table while($r=$empire->fetch($sql)) //Loop to get query records { echo"Title:".$r['title']."<br>" ; }
db_close(); ‐ ‐ ‐ ‐ ’ ’s ’ ’ s ’ s ’ s ’ s ’ s ’ ‐ ‐ ‐ ‐ ‐
Description of commonly used functions in the database operation class in the /e/class/db_sql.php file: 1. Execute SQL functions : $empire->query("SQL statement"); $empire->query1("SQL statement");
Description: Execution is returned successfully true, returns false if the execution is unsuccessful; The difference between the two is: an error in query() directly interrupts program execution, and an error in query1() does not interrupt program execution.
Usage example: $sql=$empire->query("select * from {$dbtbpre}ecms_news");
2. Loop to read database records Function: $empire->fetch($sql)
Description: $sql is the result returned by query execution SQL.
Usage example: $sql=$empire->query("select * from {$dbtbpre}ecms_news"); while($r=$empire->fetch($ sql)) { echo"title:".$r['title']."<br>"; }
3. Read a single database Record function: (No loop) $empire->fetch1("SQL statement")
Usage example: $r=$empire->fetch1("select * from {$dbtbpre}ecms_news where id=1"); echo"title:".$r['title'];
4. Statistics SQL query record number function: $empire->num("SQL statement") $empire->num1($sql)
Explanation: The difference between the two is: num() directly Write a SQL statement, and $sql in num1() is the result returned by query executing SQL.
Usage example: $num=$empire->num("select id from {$dbtbpre}ecms_news"); echo"The news table has a total of ".$num." news ";
5. Statistical SQL query record number function 2: (a more efficient function compared to num) $empire->gettotal("Statistical SQL statement");
Explanation: The difference between gettotal() and num() is: gettotal() uses SQL’s own count(*) function for statistics, while num() uses PHP’s own function, gettotal( ) is more efficient. The statistics in gettotal() must be as total, such as: "count(*) as total".
Usage example: $num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_news"); echo"The news table has a total of".$ num." news";
6. Get the auto-increment ID value function just inserted into the table: $empire->lastid()
Use Example: $empire->query("insert into {$dbtbpre}ecms_news(title) values('title')"); $lastid=$empire->lastid(); echo "The ID of the information just inserted is: ".$lastid;
7. Move the SQL query result record pointer: $empire->seek($sql,$pit )
Explanation: $sql is the result returned by query executing SQL, and $pit is the offset number of the pointer.
Usage example: $sql=$empire->query("select * from {$dbtbpre}ecms_news"); $empire->seek($sql,2);
8. Release SQL query result function: (generally not required) $empire->free($sql)
Instructions: $sql is the result returned by query executing SQL.
Usage example: $sql=$empire->query("select * from {$dbtbpre}ecms_news"); $empire->free($sql);
|
| |