Home  >  Article  >  CMS Tutorial  >  Summary of examples of calling database classes in PHP files to execute SQL statements under Imperial CMS

Summary of examples of calling database classes in PHP files to execute SQL statements under Imperial CMS

silencement
silencementforward
2019-11-29 11:53:463074browse

Summary of examples of calling database classes in PHP files to execute SQL statements under Imperial CMS

Example 1: Example of connecting to MYSQL database. (a.php)

The code is as follows:

<?php
require(&#39;e/class/connect.php&#39;); //引入数据库配置文件和公共函数文件
require(&#39;e/class/db_sql.php&#39;); //引入数据库操作文件
$link=db_connect(); //连接MYSQL
$empire=new mysqlquery(); //声明数据库操作类</p> <p>db_close(); //关闭MYSQL链接
$empire=null; //注消操作类变量
?>

Example 2: Update data example. (b.php)

The code is as follows:

<?php
require(&#39;e/class/connect.php&#39;); //引入数据库配置文件和公共函数文件
require(&#39;e/class/db_sql.php&#39;); //引入数据库操作文件
$link=db_connect(); //连接MYSQL
$empire=new mysqlquery(); //声明数据库操作类</p> <p>$empire->query("update {$dbtbpre}ecms_news set 
onclick=onclick+1"); //给新闻表的点击数加1</p> <p>db_close(); //关闭MYSQL链接
$empire=null; //注消操作类变量
?>

Recommended to study "Empirecms Tutorial"

Example 3: Example of querying MYSQL data. (c.php)

The code is as follows:

<?php
require(&#39;e/class/connect.php&#39;); //引入数据库配置文件和公共函数文件
require(&#39;e/class/db_sql.php&#39;); //引入数据库操作文件
$link=db_connect(); //连接MYSQL
$empire=new mysqlquery(); //声明数据库操作类</p> <p>$sql=$empire->query("select * from {$dbtbpre}ecms_news 
order
 by newstime limit 10"); //查询新闻表最新10条记录
while($r=$empire->fetch($sql)) //循环获取查询记录
{
echo"标题:".$r[&#39;title&#39;]."
";
}</p> <p>db_close(); //关闭MYSQL链接
$empire=null; //注消操作类变量
?>

/e/class/db_sql.php file Commonly used function descriptions for database operation classes:

1. Execute SQL functions :

The code is as follows:

$empire->query("SQL语句");
$empire->query1("SQL语句");

Explanation:

Returns true if the execution is successful, false if the execution is unsuccessful;
The difference between the two is: query() interrupts directly when an error occurs Program execution, query1() error does not interrupt program execution.

Usage example:

The code is as follows:

$sql=$empire->query("select * from {$dbtbpre}ecms_news");

2. Loop reading database record function:

The code is as follows:

$empire->fetch($sql)

Description:

$sql is the result returned by query executing SQL.

Usage example:

The code is as follows:

$sql=$empire->query("select * from {$dbtbpre}ecms_news");
while($r=$empire->fetch($sql))
{
echo"标题:".$r['title']."
";
}

3. Read a single database record function: (not looping)

The code is as follows:

$empire->fetch1("SQL语句")

Usage example:

The code is as follows:

$r=$empire->fetch1("select * from {$dbtbpre}ecms_news where id=1");
echo"标题:".$r[&#39;title&#39;];

4. Statistics SQL query record number function:

The code is as follows:

$empire->num("SQL语句")
$empire->num1($sql)

Explanation:

The difference between the two is: num() writes SQL statements directly, while $sql in num1() is the result returned by query executing SQL.

Usage example:

The code is as follows:

$num=$empire->num("select id from {$dbtbpre}ecms_news");
echo"新闻表共有 ".$num." 条新闻";

5. Statistical SQL query record number function 2: (a more efficient function compared to num)

The code is as follows:

$empire->gettotal("统计SQL语句");

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:

The code is as follows:

$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_news");
echo"新闻表共有 ".$num." 条新闻";

6. Get the auto-increment ID value function just inserted into the table:

The code is as follows:

$empire->lastid()

Usage example:

The code is as follows:

$empire->query("insert into {$dbtbpre}ecms_news(title) values('标题')");
$lastid=$empire->lastid();
echo"刚插入的信息ID为:".$lastid;

7. Move the SQL query result record pointer:

The code is as follows:

$empire->seek($sql,$pit)

Description:
$sql is the result returned by query executing SQL, and $pit is the offset number of the pointer.

Usage example:

The code is as follows:

$sql=$empire->query("select * from {$dbtbpre}ecms_news");
$empire->seek($sql,2);

8. Release the SQL query result function: (Generally no need to use)

The code is as follows:

$empire->free($sql)

Description:

$sql is the result returned by query executing SQL.

Usage example:

The code is as follows:

$sql=$empire->query("select * from {$dbtbpre}ecms_news");
$empire->free($sql);

Navigation query example:

The code is as follows:

<DIV class=news_dh style="color:#000000"></p> <p><!--主栏目导航,此模板中直接指定为classid=69,即法律法规栏目-->
[e:loop={"select classid,classname,classpath from [!db.pre!]enewsclass where classid=&#39;69&#39; and showclass=0 order
 by myorder ",0,24,0}]
<span><a href="<?=$public_r[newsurl]?><?=$bqr[classpath]?>" ><?=$bqr[classname]?></a></span>   
<SPAN><IMG
src="[!--news.url--]skin/tx/zixun/images/index_line_nav.gif"></SPAN>
[/e:loop]</p> <p> </p> <p><!--子栏目导航-->
[e:loop={"select classid,classname,classpath from [!db.pre!]enewsclass where bclassid=&#39;69&#39; and showclass=0 order 
by myorder ",0,24,0}]
<?
$lanmuclass= "lanmu";
//echo $bqr[classid] ;
//echo "cur:".$GLOBALS[navclassid];
if ( $bqr[classid]==$GLOBALS[navclassid]) {
//echo "-";
$lanmuclass ="curlanmu";
}
?></p> <p><span class="<?=$lanmuclass?>"><a href="<?=$public_r[newsurl]?><?=$bqr[classpath]?>" >
<?=$bqr[classname]?></a></span>   <SPAN><IMG
src="[!--news.url--]skin/tx/zixun/images/index_line_nav.gif"></SPAN></p> <p>[/e:loop]</p> <p>
<!--新闻导航结束-->
</DIV>

The above is the detailed content of Summary of examples of calling database classes in PHP files to execute SQL statements under Imperial CMS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.word666.com. If there is any infringement, please contact admin@php.cn delete