Three databases for learning and practicing Ajax and PHP

Source: Internet
Author: User

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, but this information is for testing convenience to write data in the array in advance, it cannot 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.

Bottom Point SourceProgramDbconnector. php In The package provides the connection function with MySQL.

 <? PHP // 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. $ Predictionstring =" Error connection to database: <br/> "; $ 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.

 
 
 <? PHP // 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 ){ ?> < Div Style = "Width: 150px; Background: # ffbc37; border-style: solid;
Border-color: #000000; border-width: 1px ;"
> < Div Style = "Padding: 10px ;" > <? PHP // Display memorandum information While ($ datedata = mysql_fetch_array ($ datequery) {If (! Get_magic_quotes_gpc () {echo stripslashes ($ datedata [' Description ']);} Else {echo $ datedata [' Description '];} ?> </ Div > </ Div > <? PHP} 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.

Source codeDownload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.