Home >Backend Development >PHP Tutorial >How to use the database class that comes with Empire CMS_PHP Tutorial

How to use the database class that comes with Empire CMS_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:071444browse

Empire CMS encapsulates a SQL, and the file location is e/class/db_sql.php; using the class library of the program itself can bring convenience and efficiency to our development, and at the same time reduce the number of additional files.

First, make the preparations:

  1. Create a test directory under the e directory, for example, I created e/trylife/td-test/
  2. Create a PHP file in the directory 1, for example, I created e/trylife/td/test-db_sql.php
  3. The code written in the file is as follows:
<?php  
/*引用文件*/  
include("../../class/connect.php");  
include("../../class/db_sql.php");  
  
/*建立数据库链接 与 实例化类*/  
$link=db_connect();  
$empire=new mysqlquery();  
  
	/*中间的这个位置用于我们测试代码*/  
  
/*关闭数据库连接 与 释放类*/  
db_close();  
$empire=null;  
?>  

The first test object: query

  1. query() executes mysql_query()
  2. The return value also follows the explanation of mysql_query() in the PHP manual, but if the execution fails, it is different from mysq_query
  3. The test code is as follows (removing the lengthy comments):
<?php  
include("../../class/connect.php");  
include("../../class/db_sql.php");  
$link=db_connect();  
$empire=new mysqlquery();  
  
function hr(){  
     echo ' 
<hr /-->';  
}  
  
$sql=$empire->query("select id,title from {$dbtbpre}ecms_news");  
var_dump($sql);  
hr();  
  
//如语句执行成功则返回true  
$sql=$empire->query("UPDATE {$dbtbpre}ecms_news set title='标题' where id=1");  
var_dump($sql);  
hr();  
  
//如语句执行失败则终止执行并返回错误语句 下面的语句用了不存在的字段  
$sql=$empire->query("UPDATE {$dbtbpre}ecms_news set titlesss='标题' where id=1");  
var_dump($sql);  
hr();  
  
db_close();  
$empire=null;  
?>

The source text about the query object is as follows: In the ninth line of e/class/db_sql.php; the PHP manual for die() explains that "the die() function outputs a message and exits the current script"; so the third var_dump() and hr() under the test statement exited without executing;

function query($query)  
{  
    $this->sql=mysql_query($query) or die(mysql_error().''.str_replace($GLOBALS['dbtbpre'],'***_',$query));  
    return $this->sql;  
}  

The second test object: query1

The object query1 is the same as mysql_query(), and the longer Chinese characters have been deleted to save space.

<?php  
include("../../class/connect.php");  
include("../../class/db_sql.php");  
$link=db_connect();  
$empire=new mysqlquery();  
  
function hr(){  
    echo ' 
<hr /-->';  
}  
  
$sql=$empire->query1("select id,title from {$dbtbpre}ecms_news");  
var_dump($sql);  
hr();  
  
//如语句执行成功true  
$sql=$empire->query1("UPDATE {$dbtbpre}ecms_news set title='测试更新标题' where id=1");  
var_dump($sql);  
hr();  
  
//如语句执行失败则返回FLASE  
$sql=$empire->query1("UPDATE {$dbtbpre}ecms_news set titlesss='测试更新标题' where id=1");  
var_dump($sql);  
hr();  
  
db_close();  
$empire=null;  
?>  

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752553.htmlTechArticleEmpire CMS encapsulates a SQL, the file location is e/class/db_sql.php; use the class library of the program itself It can bring convenience and efficiency to our development, and at the same time, it can reduce the number of additional files. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn