Var cek = {
checkUser : function(user) {
if(!$('modify').value || $('default_user').value != $(user).value) {
var url = "?mod=admin&file=sys&method=checkusername";
//var url = 'test.php’;
//var url = '../private/zend/index.php';
var pars = '';
var myAjax = new Ajax.Request(url,{method:'get',parameters:pars,onComplete:function(contents) {}});
}
}
}
方法1.以MVC模式实现连接数据库并执行查询语句的功能,?mod=admin&file=sys&method=checkusername所执行的代码如下:
复制内容到剪贴板
$application = new SysAction;
$application->checkusername();
Class sysAction {
Function checkusername() {
$link = new DbLink();
$rs = $link->checkUser(“username=’crane’”);
}
}
共创建两个对象.SysAction,DbLink,所耗时间如下:
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(63ms)
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(62ms)
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(62ms)
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(62ms)
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(62ms)
GET http://localhost/admin/?mod=admin&file=sys&method=checkusername(63ms)
方法2.直接连接数据库,并执行查询语句. test.php所执行代码如下:
复制内容到剪贴板
$db = mysql_connect('localhost','root','123456');
mysql_select_db('test');
$sql = "select * from table where username='crane'";
$result = mysql_query($sql,$db);
while($row = mysql_fetch_array($result)) {}
所耗时间如下:
GET http://localhost/admin/test.php(15ms)
GET http://localhost/admin/test.php(15ms)
GET http://localhost/admin/test.php(15ms)
GET http://localhost/admin/test.php(15ms)
GET http://localhost/admin/test.php(15ms)
GET http://localhost/admin/test.php(15ms)
GET http://localhost/private/zend/index.php(125ms)
GET http://localhost/private/zend/index.php(141ms)
GET http://localhost/private/zend/index.php(110ms)
GET http://localhost/private/zend/index.php(141ms)
GET http://localhost/private/zend/index.php(109ms)
GET http://localhost/private/zend/index.php(141ms)
测试环境:
Windows NT 5.1 build 2600
Apache 2.0
PHP 5.2.5
Mysql 5.0.45
Zend Optimizer v3.3.0