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