How to use the database class that comes with the CMS. The CMS encapsulates an SQL statement in eclassdb_ SQL .php. using the class library of the program can bring convenience and efficiency to our development, and reduce the number of files added. E/class/db_ SQL .php encapsulates an SQL statement. using the class library of the program can bring convenience and efficiency to our development, and reduce the number of files added.
First, make the following preparations:
- Create a test directory under the e Directory, for example, e/trylife/td-test/
- Create a php file under the Directory in 1, for example, I created e/trylife/td/test-db_ SQL .php
- Write the following code into the file:
First test object: query
- Query () executes mysql_query ()
- The returned value also follows the explanation of mysql_query () in the PHP Manual. However, if the execution fails, the returned value is different from that of mysq_query.
- The test code is as follows (remove the comments with a relatively large length ):
';}$ SQL = $ empire-> query ("select id, title from {$ dbtbpre} ecms_news"); var_dump ($ SQL); hr (); // if the statement is successfully executed, true $ SQL = $ empire-> query ("UPDATE {$ dbtbpre} ecms_news set title = 'title' where id = 1") is returned "); var_dump ($ SQL); hr (); // if the statement fails to be executed, terminate the execution and return the error statement. the following statement uses a non-existent field $ SQL = $ empire-> query ("UPDATE {$ dbtbpre} ecms_news set titlesss = 'title' where id = 1 "); var_dump ($ SQL); hr (); db_close (); $ empire = null;?>
The source text of the query object is as follows: in the ninth row of e/class/db_ SQL .php; view the PHP Manual for die () and interpret it as "die () function to output a message and exit the current script "; therefore, the var_dump () and its hr () in the third Test statement exit if they are not executed;
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 (). to save the layout duration, all Chinese characters are deleted.
';}$ SQL = $ empire-> query1 ("select id, title from {$ dbtbpre} ecms_news"); var_dump ($ SQL); hr (); // if the statement is successfully executed, true $ SQL = $ empire-> query1 ("UPDATE {$ dbtbpre} ecms_news set title = 'Test UPDATE title' where id = 1 "); var_dump ($ SQL); hr (); // if the statement execution fails, FLASE $ SQL = $ empire-> query1 ("UPDATE {$ dbtbpre} ecms_news set titlesss = 'Test UPDATE title' where id = 1") is returned "); var_dump ($ SQL); hr (); db_close (); $ empire = null;?>
....