在上一篇备忘日历实例中,实现了当鼠标放在某个日期上时,如果当天有备忘信息则会显示出来,但是这些信息是为了测试方便事先写在数组中的数据,不能体现其实时性。本篇将继续通过该实例讲解与数据库的交互方式。实例中用到的是MySQL,也可以根据自己的需要替换为其他数据库,其连接方式可以参考PHP相关手册。
在下面源程序包中dbconnector.php 提供了与MySQL的连接函数。
//定义数据连接变量
define ("MYSQLHOST", "localhost");
define ("MYSQLUSER", "root");
define ("MYSQLPASS", "root");
define ("MYSQLDB", "test");
function opendatabase(){
//连接数据库所在服务器
$db = mysql_connect (MYSQLHOST,MYSQLUSER,MYSQLPASS);
try {
if (!$db){
//若无法连接则提示错误
$exceptionstring = "Error connection to database: <br />";
$exceptionstring .= mysql_errno().": ".mysql_error();
throw new exception ($exceptionstring);
}
else{
//连接数据库(test)
mysql_select_db (MYSQLDB,$db);
}
return $db;
}catch (exception $e){
echo $e->getmessage();
die();
}
}
?>
当鼠标放到某个日期上时会调用functions.js中的checkfortasks函数。同时checkfortasks会加载taskchecker.php程序,它会到MySQL中查询该日期下所有的备忘录信息,并将结果返回到页面中。
//调用数据库连接程序
require_once ("dbconnector.php");
//连接数据库
$db = opendatabase();
//在MySQL查询备忘录
$querystr = "SELECT description FROM task WHERE thedate=’" . addslashes ($_GET[‘thedate’]) . "’";
//执行SQL
if ($datequery = mysql_query ($querystr)){
//判断查询是否有值
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
//显示备忘录信息
while ($datedata = mysql_fetch_array ($datequery)){
if (!get_magic_quotes_gpc()){
echo stripslashes ($datedata[‘description’]);
}
else{
echo $datedata[‘description’];
}
}
?>
</div>
</div>
<?php
}
}
else{
//数据库查询错误
echo mysql_error();
}
//关闭数据库
mysql_close ($db);
?>
对于Ajax的使用和上一篇的原理是一样的:1. 通过事件调用Ajax函数;2. 通过函数请求其他PHP程序,PHP程序中可以对数据库之类的数据源进行读、写、改操作;3. 将处理结果加载到事件激发页面。在下图中鼠标放到26号时,Ajax会在MySQL中查询到“Football Match”事件并加载到当前页面。
<img title="tasklist" height="229" alt="tasklist" src="http://www.wfuyu.com/uploadfile/phpjc/www.cnblogs.com/gnielee/
波比源码 » Ajax & PHP 边学边练 之三 数据库
levaquin 250mg generic buy levaquin sale
olmesartan 10mg sale buy depakote online buy diamox 250 mg pill
isotretinoin 10mg price accutane oral brand deltasone 40mg
orlistat 120mg over the counter zovirax canada order acyclovir 800mg generic
order generic tadalafil cialis 5mg oral order plavix
buy diflucan 200mg buy viagra 50mg generic oral sildenafil
ceftin 250mg over the counter bimatoprost for sale buy generic robaxin
buy lasix 100mg online cheap lasix 40mg uk plaquenil 400mg over the counter
order naproxen pills oral lansoprazole 30mg purchase prevacid online cheap
tadalafil 20mg tablet Cialis overnight delivery free spins no deposit casino
roulette game blackjack online real money slots free online
order carvedilol sale generic ditropan 5mg order elavil pill
alendronate for sale online order alendronate 70mg online oral ibuprofen 600mg
order priligy 60mg pills purchase avana pill motilium 10mg tablet
doxycycline 100mg us brand doxycycline methylprednisolone medicine
requip cheap calcitriol pill labetalol 100mg uk
Thank you for content. Area rugs and online home decor store. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
order tricor 160mg sale sildenafil cost sildenafil overnight shipping
tadalafil 20mg generic Cialis visa viagra usa
buy minocycline 100mg without prescription gabapentin 100mg pill hytrin 5mg ca
cialis 20mg pill best ed drug cheapest ed pills
buy metformin 500mg nolvadex canada buy tamoxifen sale
buy modafinil 100mg pills modafinil 200mg uk phenergan drug
buy serophene for sale buy generic clomid buy prednisolone 20mg online
purchase isotretinoin online purchase ampicillin generic ampicillin 250mg without prescription
free samples of ed pills fildena 50mg tablet propecia for sale online
buy ivermectin 6 mg online order deltasone 10mg online purchase deltasone pills
buy ondansetron online cheap zofran without prescription cost trimethoprim