Smarty combined with Ajax to implement a new message book without refreshing the example and read the title. you may want to say that the message book is very basic! No one will, but still use Smarty. isn't it tiring? Don't worry. what I want to express is a programming idea and structure, instead of proving how meaningful I am. through it, I believe it is helpful for beginners to learn about Smarty and ajax. Originally done with ajax, but it is a pity that debugging has never been successful, so I had to write JavaScript code. but it doesn't matter, but it still has some value. You can see the source code in the site structure. the code is not long and it should not be annoying.
Now all PHP5 OO (Object Oriented) is very popular, and it is not bad here. First of all, let's take a look at using OO to implement database operations and connections:
[Php]
/**************************
Page: dbclass. php
Author: Boss Hui
Function: defines database operations.
**************************/
Php
/**************************
Page: dbclass. php
Author: Boss Hui
Function: defines database operations.
**************************/
ClassDb{
// Create a constructor to connect to the database and select a database
Public function_ Construct(){
Require ('Config. inc. php');
Mysql_connect($ Dbhost,$ Dbuser,$ Dbpassword) Or die ("Error! ");
Mysql_query("Set names 'gbk '");
Mysql_select_db($ Dbname);
}
// Execute SQL statement functions
Public functionQuery($ SQL){
ReturnMysql_query($ SQL);
}
// Obtain the result set number Group function
Public functionLoop_query($ Result){
ReturnMysql_fetch_array($ Result);
}
// Create a destructor to disable database connection
Public function_ Destruct(){
ReturnMysql_close();
}
}
?>
What are the characteristics of this class? First, we will introduce _ construct () as a constructor. what is a constructor? In layman's terms, the class is automatically executed after being instantiated, __destruct () What is it? It is a destructor. its function is to automatically destroy an object without any method pointing to this object. it generally contains some final operations, such as closing a file, do you understand how to close the database connection? That's right! The constructor with the database connection method is automatically executed during class instantiation, and the destructor that closes the database connection when the instance is destroyed, for some basic data operations, we only need to create a new $ db object, and then $ db-> query ()... is it very convenient, of course, this is just a simple example, you can continue to expand. Let's see what is in config. inc. php:
It's easy to be right. if you are interested, let's take a look at ^ _ ^. let's take a look at the template file:
Php
/*************************
Page: config. inc. php
Author: Boss Hui
Function: Sets database parameters and variables.
$ Dbhost: host name
$ Dbuser: connection account
$ Dbpassword: connection password
$ Dbname: database name
*************************/
$ Dbhost="Localhost";
$ Dbuser="Root";
$ Dbpassword="7529639";
$ Dbname="Testdb";
?>
<{$ Title}>