Ajax & amp; PHP: Learn and practice the third database. In the last memo calendar instance, when you place the cursor over a date, if there is a memo on the day, it will be displayed, however, this information is used for testing to facilitate writing data in advance in the last memo calendar instance. when you place the cursor over a date, if there is a memo on the day, it will be displayed, however, this information is used for testing to facilitate data written in the array in advance, and does not reflect its real-time performance. This article will continue to explain how to interact with the database through this instance. MySQL is used in the instance. you can also replace it with other databases as needed. for the connection method, see the relevant PHP Manual.
In the following source package, dbconnector. php provides the connection function with MySQL.
Copy to ClipboardReference: [www.bkjia.com] // Define the data connection variable
Define ("MYSQLHOST", "localhost ");
Define ("MYSQLUSER", "root ");
Define ("MYSQLPASS", "root ");
Define ("MYSQLDB", "test ");
Function opendatabase (){
// Connect to the server where the database is located
$ Db = mysql_connect (MYSQLHOST, MYSQLUSER, MYSQLPASS );
Try {
If (! $ Db ){
// If the connection fails, an error is displayed.
$ Exceptionstring = "Error connection to database:
";
$ Exceptionstring. = mysql_errno (). ":". mysql_error ();
Throw new exception ($ exceptionstring );
}
Else {
// Connect to the database (test)
Mysql_select_db (MYSQLDB, $ db );
}
Return $ db;
} Catch (exception $ e ){
Echo $ e-> getmessage ();
Die ();
}
}
?> When you place the cursor over a certain date, the checkfortasks function in functions. js is called. At the same time, the checkfortasks will load the taskchecker. php program, which will query all the memorandum information under this date in MySQL and return the results to the page.
Copy to ClipboardReference: [www.bkjia.com] // Call the Database Connection program
Require_once ("dbconnector. php ");
// Connect to the database
$ Db = opendatabase ();
// Query the memorandum in MySQL
$ Querystr = "SELECT description FROM task WHERE thedate = '". addslashes ($ _ GET ['thedate']). "'";
// Execute SQL
If ($ datequery = mysql_query ($ querystr )){
// Determine whether the query has a value
If (mysql_num_rows ($ datequery)> 0 ){
?>
// Display memorandum information
While ($ datedata = mysql_fetch_array ($ datequery )){
If (! Get_magic_quotes_gpc ()){
Echo stripslashes ($ datedata ['description']);
}
Else {
Echo $ datedata ['description'];
}
}
?>
}
}
Else {
// Database query error
Echo mysql_error ();
}
// Close the database
Mysql_close ($ db );
?>
Ajax is used in the same way as the previous article: 1. call Ajax functions through events; 2. by using functions to request other PHP programs, PHP programs can read, write, and modify data sources such as databases. 3. load the processing result to the event activation page. When you place the cursor on the 26th, Ajax will query the "Football Match" event in MySQL and load it to the current page.
Download source code: Sample4.rar
Author Blog: http://www.cnblogs.com/gnielee/
...