Ç°ÑÔ
ΪʲôҪʹÓÃÊý¾Ý¿âÀà¿â?
³õѧÕß (ÉõÖÁһЩ·Ç³õѧÕß)³£³£·¸µÄ´íÎ󣬾ÍÊÇÔÚ¿ª·¢Ó¦ÓóÌÐòµÄʱºò£¬È±·¦¶ÔδÀ´µÄ¿¼ÂÇ¡£¼ÙÈçÓÐÒ»Ì죬ÄãµÄ³ÌÐòÐèҪʹÓÃÆäËüµÄÊý¾Ý¿â£¬ÄѵÀÄ㻹ÐèÒªÖØÐÂдһƪÕë¶ÔÓÚÕâ¸ö°æ±¾Êý¾Ý¿âµÄ³ÌÐòÂð£¿Õâ²»ÊDz»¿ÉÄÜ·¢ÉúµÄ£¬ÓÈÆäÊǵ±Ä¿Ç°µÄÊý¾Ý¿â¿ÉÄܲ»ÊʺÏÄ㵱ǰµÄÐèÒªµÄʱºò¡£ µ«Êǵ±Äã×öÕâÒ»ÇеÄʱºò ,Äã»á·¢ÏÖ¾ø·ÇÄãÏëÏñµÄÄÇÑùÈÝÒ×£ºÃ¿Ò»¸ö DBMS Óв»Í¬µÄº¯Êý¡£¾ÙÀýÀ´Ëµ£ºÔÚ Mysql ÖÐÁ¬½ÓÊý¾Ý¿âµÄº¯ÊýΪ mysql_connect(),¶øÔÚMSSQLÈ´ÊÇ mssql_connect()¡£ÖØв鿴ÄãËùÓеĴúÂ룬ȻºóÐÞ¸ÄËùÓвÙ×÷Êý¾Ý¿âµÄº¯ÊýºÍ²éѯÓï·¨£¬Õâ¾ø²»ÊÇÒ»Ïî¼òµ¥µÄ¹¤×÷¡£³ÌÐò»á¾³£µÄ±»À¦°óµ½ÁË´íÎóµÄÊý¾Ý¿âÉÏ£¬ÒÔÖÁÎÞ·¨ÔÚÔËÐÐʱ´ïµ½×îÓÅÐÔÄÜ¡£Êý¾Ý¿âÀà¿â¾ÍÊÇÕâÑù²úÉúµÄ¡£ËüÔÊÐíÄãͨ¹ýͬÑùµÄ´úÂëÀ´²Ù×÷²»Í¬µÄÊý¾Ý¿â¡£Ò»¸öÉè¼ÆÁ¼ºÃµÄÊý¾Ý¿âÀà¿â¿ÉÒÔÍêÃÀµÄ¸Ä±äÕâÒ»ÇС£ËüÔÊÐíÄãͨ¹ý¼«Ð¡µÄÐ޸ģ¬¾ÍÄÜת½Óµ½ÆäËûÊý¾Ý¿â£ºÎÞÂÛÄãÒªÁ¬½ÓʲôÊý¾Ý¿â¹ÜÀíϵͳ£¬Ö»ÒªÊ¹ÓÃͬһ¸öµÄº¯Êý£¬ºÍ²»Í¬µÄ²ÎÊý¡£¾ÙÀýÀ´Ëµ, ÔÚ ADOdb ÖÐ,Ö»Ðè¼òµ¥µØ½« $db = NewADOConnection('access ') ÐÞ¸ÄΪ$db = NewADOConnection('mysql'),ÕâÑù£¬Äã¾ÍÇáËɵİÑÄãµÄ³ÌÐò´Ó Access Êý¾Ý¿âתÒƵ½ÁË Mysql Êý¾Ý¿âÖС£ÏÖÔÚÍøÂçÉÏÒѾÓÐÁ˺ܶàÕâÑùµÄÊý¾Ý¿âÀà¿â£¬±ÈÈçPear£¬PHPlib£¬ÎÒÒ²ÒѾÔÚ¹¤×÷ÖÐʹÓÃËüÃÇÁË£¬Ò²ÐíÄãÒ²Ôø¾Óùý¡£µ«±¾ÎĽö½«Öصã·ÅÔÚÎÒËùÌرð¹Ø×¢µÄ ADOdb¡£±¾ÎÄÎÒÖ»¼òÒªµØ×÷Ò»¸ö½éÉÜ£¬Ê¹ÄãÄܹ»ÂíÉÏÔËÓÃËüÀ´¿ª·¢ÄãµÄÏÂÒ»¸öÏîÄ¿¡£ÒÔºóµÄÎÄÕÂÖÐÎÒÃǽ«»áÑÐò½¥½øµÄ¶ÔËü½øÈë¸üÉîÈëµÄÁ˽⡣
Ä¿Ç°£¬ADOdb Ö§³ÖµÄÊý¾Ý¿â°üÀ¨ MySQL, PostgreSQL£¬Interbase£¬Firebird£¬Informix£¬Oracle£¬MS SQL 7£¬Foxpro£¬Access£¬ADO£¬Sybase£¬FrontBase£¬DB2 ºÍ generic ODBC¡£
ADOdb µÄ°²×°
°²×° ADOdb ÊÇÒ»¼þ¼«ÆÚÈÝÒ×µÄÊ£¬ÏàÐÅ´ÏÃ÷µÄÄãÒ»¶¨²»»á¸Ðµ½³ÔÁ¦¡£
Ê×ÏÈ, È·¶¨ÄãÕýÔÚÔËÐÐµÄ PHP ÊÇ 4.0.4 °æ»ò¸üа档 Èç¹û²»ÊÇ£¬ÎÒÇ¿Áн¨ÒéÄãÉý¼¶!
´Ó PHP Everywhere Õ¾µãÏÂÔØ .zip »ò .tgz Îļþ£¬½âѹËõµ½ÄãËùÑ¡µÄ·¾¶Ï¡£
Õâ¸ö·¾¶²»Ó¦ÔÚÍøҳĿ¼£¨WWWTREE,ÒëÕß×¢£ºÈç¹ûÄãµÄÍøÒ³ÊÇÔÚ/www/Ï£¬ÄÇô£¬Õâ¸öĿ¼¾Í²»Ó¦Îª/www/here£©Ï£¡ËäÈ»ADOdbµÄ°üº¬ÎļþÒѾʹÓÃÁË .inc.php µÄÀ©Õ¹Ãû £¬Ê¹µÃ·þÎñÆ÷¼´Ê¹ÊÇÔÚ×îÔã¸âµÄÅäÖÃÏ£¬Ò²²»»á½«ÕâЩ.incÎļþͨ¹ýÃ÷ÎÄ·½Ê½ÔÚä¯ÀÀÆ÷ÖÐÏÔʾ³öÀ´£¬µ«ÊÇÎÒÃÇÏòÀ´²»Ìᳫ½«¿âº¯ÊýÎļþ·ÅÔÚÍøҳĿ¼ÏµÄÐÐΪ¡£È»ºó°ÑÏÂÔØÏÂÀ´µÄÎļþÔËÐУºtar -zxvf adodb350.tgz ½âѹ£¬ÔÚWindowsÏÂÄã¿ÉÒÔʹÓÃÒ»¸öÄãϲ»¶µÄ½âѹÈí¼þÀ´²Ù×÷£¬ÕâÑù£¬Äã»áµÃµ½Ò»¸ö adodb µÄĿ¼ÆäÏ°üÀ¨ÁËÐí¶à×ÓĿ¼¡£
²âÊÔÄãµÄ°²×°
ºÃÁË£¬ÈÃÎÒÃÇÀ´²âÊÔÒ»ÏÂÄãµÄ°²×°°É¡£ ͨ¹ýÔڽű¾ÖÐÌí¼ÓÏÂÁÐÈýÐдúÂëÀ´²âÊÔÄãµÄ°²×°ÊÇ·ñ³É¹¦¡£×¢ÒâÒª°Ñ´úÂëÖеIJÎÊýÐ޸ijÉÄã×Ô¼ºµÄ¡£
include("$adodb_path/adodb.inc.php"); // includes the adodb library
$db = NewADOConnection('$database_type'); // A new connection
$db->Connect("$host", "$user", "$password", "$database_name");
ÏÖÔÚÄãÒѾӵÓÐÒ»¸öÊý¾Ý¿âÁ¬½Ó¶ÔÏó $db ÁË¡£ ÄãÒ²¿ÉÒÔʹÓà ADONewConnection À´Ìæ»» NewADOConnection ---- ÕâÁ½¸öÊÇͬһº¯ÊýµÄ²»Í¬µÄÃû×Ö¡£ Á¬½ÓµÄÊý¾Ý¿â±äÁ¿ $database_type ÐèÒªÕë¶ÔÄãµÄʵ¼ÊÇé¿ö¸Ä³ÉÄãËùÐèÒªµÄ¡£¿ÉÒÔʹÓÃÒÔÏÂÁбíÖеÄÒ»¸ö(À¨ºÅÄÚµÄΪÃèÊö²¿·Ö£¬²»ÒªÔÚ´úÂëÖÐʹÓÃ)£º
access (Microsoft Access/Jet)
ado (Generic ADO, the base for all the other ADO drivers)
ado_access (Microsoft Access/Jet using ADO)
ado_mssql (Microsoft SQL Server using ADO)
db2 (DB2)
vfp (Microsoft Visual FoxPro)
fbsql (FrontBase)
ibase (Interbase 6 or before)
firebird (Firebird)
informix72 (Informix databases before Informix 7.3)
informix (Informix)
maxsql (MySQL with transaction support)
mssql (Microsoft SQL Server 7)
mssqlpo (Portable mssql driver)
mysql (MySQL without transaction support)
mysqlt (MySQL with transaction support, identical to maxmysql)
oci8 (Oracle 8/9)
oci805 (Oracle 8.0.5)
oci8po (Oracle 8/9 portable driver)
odbc (Generic ODBC, the base for all the other ODBC drivers)
odbc_mssql (MSSQL via ODBC)
odbc_oracle (Oracle via ODBC)
oracle (Oracle 7)
postgres (PostgreSQL)
postgres64 (PostgreSQL 6.4)
postgres7 (PostgreSQL 7, currently identical to postgres )
sqlanywhere (Sybase SQL Anywhere)
sybase (Sybase)
Èç¹ûÄãµÄÁ´½Ó´úÂë³öÏÖÁË´íÎóµÄÌáʾ£¬ÄÇôÄãÊ×ÏÈÒª¼ì²éµÄµØ·½¾ÍÊÇÔÚ·¾¶»òÁ¬½ÓµÄ±äÁ¿ÉÏ¡£ÔÚÄãÔ𱸠ADOdb ֮ǰ£¬ÇëÈ·¶¨ÄãÊÇÒѾÕýÈ·µÄʹÓÃÄÇЩ±äÁ¿¡£(ºÜ¶àÅóÓѳ£»¨Ì«¶àʱ¼äÈ¥ÐÞÕýÕâЩÏÔ¶øÒ×¼ûµÄ´íÎó¡£) Èç¹ûÁ¬½ÓûÓÐÈκδíÎóÌáʾ£¬ÄÇôÎÒÃÇÏÖÔÚÒѾ¿ÉÒÔÔÚÎÒÃǵÄÏîÄ¿ÖÐÀ´Ê¹Óà ADodb ÁË¡£
ͨ¹ýÄãµÄ½Å±¾Á¬½Óµ½Êý¾Ý¿â
°ÑÉϱߵĴúÂë¼ÓÈëµ½ÄãµÄ´úÂëÇ°£¬ÈÃÎÒÃÇÏÈÍËÒ»²½¡£ÎÒÃÇ×îºÃÄÜ°ÑÉϱߵĴúÂëÓÃÎÒÃÇ×Ô¼ºµÄ·½·¨À´·â×°ÆðÀ´¡£ÕâÑù¿ÉÒÔʹÄãµÄ³ÌÐò±äµÃ¸üÁé»î¡¢¸ü¾ßÒÆÖ²ÐÔ¡£Èç¹ûÄãÖ±½Ó°ÑÉÏÃæµÄ´úÂë²åÈëµ½ÄãµÄÏîÄ¿µÄÿ¸öÎļþÖУ¬Èç¹û½«À´ÏîÄ¿µÄ·¾¶¸Ä±äÁË£¬½«»áºÜÈÝÒײúÉú´íÎó£¬Èç¹ûÄãµÄÃÜÂë¸Ä±äÁË£¬Äã¿ÉÄÜÐèÒª¶ÔÄãËùÓеĽű¾½øÐÐÐ޸ģ¬ÕâÑù½«»áÓ°ÏìÎÒÃÇʹÓÿ⺯ÊýµÄ³õÖÔ¡£²¢ÇÒ£¬ÒòΪÄãµÄÃÜÂëÐÅÏ¢ÊÇÔÚWEBTREEϵģ¬Õ⽫²úÉúÒþ»¼¡£ÎÒÍƼö½«ÃÜÂëÐÅÏ¢·ÅÔÚÒ»¸ö¶ÀÁ¢µÄ°üº¬ÎļþÖУ¬±ÈÈçÔÚ ADOdb °²×°Ä¿Â¼ÏµÄij¸öµØ·½¡£Èç¹ûÄãÒªÔÚÆäËû·þÎñÆ÷ÉÏÔËÐÐÄãµÄ³ÌÐòʱ£¬Äã¾Í²»Äܱ£Ö¤Ä¿Â¼½á¹¹»áÊÇÏàͬµÄ£¬ËùÒÔ£¬ÄãҪȷ±£Õâ¸ö·¾¶ÊÇÕýÈ·µÄ¡£ÎÒ½¨ÒéʹÓÃPHP µÄ×Ô¶¯°üº¬¹¦ÄÜÀ´×Ô¶¯µØ°üº¬Õâ¸öÎļþ¡£
include("$adodb_path/db_values.inc.php");
include("$adodb_path/adodb.inc.php");
$db = NewADOConnection('$database_type');
$db->Connect("$host", "$user", "$password", "employees");
Èç¹ûÄãÒ²ÏëҪʹÓó־ÃÁ¬½Ó, ²»ÊÇÿ´Î´´ÔìÒ»¸öеÄÁ¬½Ó(ÕâʹÐí¶àWEBÓ¦ÓóÌÐòµÃµ½Á˼ÓËÙ£¬µ«ÊÇҪעÒâÓÐЩÊý¾Ý¿âÊDz»Ö§³ÖµÄ)¡£¿ÉÒÔʹÓà PConnect Ìæ»»µô Connect¡£
Îļþ db_values.inc.php ÊÇÎÒÃǵÄÊý¾Ý¿âÐÅÏ¢Îļþ£¬ÄÚÈÝΪ(ÄãÐèÒª°ÑÏÂÃæ´úÂëÖеıäÁ¿¸Ä³ÉÄã×Ô¼ºµÄ):
$database_type="mysql";
$host = "localhost"; // ±¾µØÊý¾Ý¿â
$user = "ian"
$password = "let_me_in"
?>
Äã¿ÉÒÔÔÚ php.ini ÅäÖÃÖÐÉ趨×Ô¶¯°üº¬ÎÒÃǵÄÅäÖÃÎļþ£¬¾ßÌå²Ù×÷¿ÉÒÔÐÞ¸Ä PHP.ini µÄÏÂÊöÐУº
; Automatically add files before or after any PHP document.
auto_prepend_file = /usr/local/build/apache/www/tool_lib/defaults.inc
auto_append_file =
Îļþ defaults.inc °üº¬ÁË $adbdb_path µÄÖµ£º
$adodb_path = "/usr/local/build/apache/www/tool_lib/";
?>
»¹ÓÐÆäËû·½Ê½À´ÊµÏÖËü£¬µ«ÊÇÎÒ·¢ÏÖÕâÖÖ·½·¨ÔÚÒÆֲʱ£¬¿ÉÒÔÏà¶ÔµØ¼õÉÙ¸´ÔӶȡ£
´ÓÒ»¸öÊý¾Ý¿âÖнøÐÐÑ¡Ôñ(SELECT)²Ù×÷
µ±Í¬Ê±Ê¹Óÿª·¢Á¼ºÃµÄ¿âº¯Êý£¬ºÍPHP×ÔÉíÌṩµÄº¯Êýʱ£¬¿ÉÒÔÓжàÖÖ·½·¨À´·ÃÎÊÊý¾Ý¿â¡£ÓÃʲô·½·¨£¬ÍêÈ«È¡¾öÓÚÄã×Ô¼ºµÄϲºÃ¡£
ÕâÀïÊÇÒ»¸ö¼òµ¥µÄÀý×Ó:
$sql = "SELECT surname, age FROM employees";
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'
';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
}
}
ÔÚÉÏÀýÖÐ,$rs->fields ÊÇÒ»¸ö°üº¬·µ»ØÖµµÄÊý×é¡£Êý×éË÷Òý±»¸³ÓèÁ˳õʼµÄÊý×Ö£¬ÄãÒ²¿ÉÒÔ°´ÏÂÃæµÄ·½·¨À´Ö¸¶¨ÆäË÷Òý:
$sql = "SELECT surname, age FROM employees";
$db->SetFetchMode(ADODB_FETCH_ASSOC); // Return associative array
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields['surname']." ".$rs->fields['age']."
";
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
¶Ô½á¹ûä¯ÀÀµÄÁíÒ»¸ö¿ÉÑ¡µÄ·½·¨Êǽ«Ã¿Ìõ¼Ç¼×÷Ϊһ¸ö¶ÔÏ󷵻ء£ ADOdb ÓÐÒ»¸ö FetchNextObject() µÄº¯ÊýÀ´ÊµÏÖÕâÒ»¹¦ÄÜ,Ö¸Õë»á×Ô¶¯µØÒƵ½ÏÂÒ»Ìõ¼Ç¼¡£
$sql = "SELECT surname, age FROM employees";
$db->SetFetchMode(ADODB_FETCH_ASSOC); // Return associative array
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
// loop through results
while ($row = $rs->FetchNextObject()) {
// The field names need to be uppercase
print $row->SURNAME." ".$row->AGE."
";
}
²åÈë¡¢¸üмǼ
»ù±¾µÄ INSERT ²Ù×÷·½±ã¡¢¿ì½Ý, ÓµÓÐÓëSELECTÒ»ÑùµÄÓï·¨¡£
$sql = "INSERT INTO employees (surname, age) values ('Clegg','43')";
if (!($db->Execute($sql))) {
print 'Error inserting: '.$db->ErrorMsg().'
';
}
¿âº¯ÊýÕæÕýµÄÓŵ㣬ÔÚÓÚËüÔÊÐíÄãͨ¹ýÏàͬµÄÓï·¨½«¼Ç¼·ÅÈ벻ͬµÄÊý¾Ý¿âÖ®ÄÚ£¬ÕâÔÚÒÔÇ°ÊǾø¶Ô²»¿ÉÄܵġ£Í¨³£ÓÐÁ½ÖÖ·¢ÉúµÄÇéÐΡ£
µÚÒ»ÖÖ,ÒýºÅ¡£ËùÓеÄÒýºÅÐèÒªÓÃÍÑ×Ö·û(¼´`·ûºÅ,¼üλÔÚTab¼üµÄÉϱß)´úÌ棬·ñÔò»áÒýÆðÓï·¨´íÎó¡£µ«ÊÇһЩÊý¾Ý¿âʹÓÃÒ»¸öµ¥ÒýºÅ,ÁíÍâһЩÔòʹÓÃÁ½¸öµ¥ÒýºÅ¡£ËùÒÔ£¬ÄãÓ¦µ±Ê¹Óà ADOdb ÖÐµÄ qstr() ¶ø²»ÊÇ PHP ÖÐµÄ addslashes()¡£ÕâÑù£¬·µ»ØÖµ¾Í½«ÓëÄãËùʹÓõÄÊý¾Ý¿âÏàÎǺÏÁË¡£
µÚ¶þÖÖ,ÈÕÆÚ¡£Ðí¶àÊý¾Ý¿â½ÓÊܸúËûÃǵÄÈÕÆÚÀàÐͲ»Ò»Öµġ¢²»¼æÈݵĸñʽ¡£ ADOdb ÓÐÒ»¸ö DBDate() º¯Êý,¿ÉÒÔ½« Unix µÄ timestamp, »ò ISO(Y-m-d) ¸ñʽת»»³ÉÈÎÒâ¸ñʽ£¬ÒÔ´ËÀ´Âú×ãÄãµÄÊý¾Ý¿âµÄÐèÇó¡£ ¼ûÏÂÀý£º
$employee_surname = $db->qstr("d'Angelo");
$arrival_time = $db->DBDate(time());
// The above two functions also add the enclosing quotes, so, $arrival_time, not '$arrival_time'
$sql = "INSERT INTO employee_arrival (arrival_time,surname) values ($arrival_time,$employee_surname)";
if (!($db->Execute($sql))) {
print 'Error inserting: '.$db->ErrorMsg().'
';
}
Äã¿ÉÒÔÒÔÍêÈ«ÏàͬµÄ·½Ê½¸üÐÂÊý¾Ý¿â,¾ÙÀýÀ´Ëµ£º
$sql = "UPDATE employees SET age='44' WHERE id='121')";
if (!($db->Execute($sql))) {
print 'Error updating: '.$db->ErrorMsg().'
';
}
ÒÔÉϽö½öÊÇ Adodb µÄһЩ»ù±¾²Ù×÷ ---- Ï´ÎÎÒÃǽ«»á¹ØעһЩ ADOdb ÌṩµÄ±È½ÏÉî²ã´ÎµÄ¶«Î÷¡£ Èç¹ûÎÒÒѾʹÄãθ¿Ú´ó¿ª£¬¶øÇÒÄãÒѾ²»ÄÜÔٵȴý£¬ ÎÒ½¨ÒéÄãÈ¥PHP Everywhere¿´Ò»ÏÂ,Õâ¸öÕ¾µãÊÇADOdbµÄרҵվµã£¬ÀïÃæÓкܶàÓÐÓõİïÖúÐÅÏ¢¡£
¶Ô²éѯ½øÐлº´æ
ÉϸöÔ£¬ÎÒÃǼòµ¥µØÁ˽âÁËADOdbÖÐ,ÈçºÎ½øÐÐSELECT¡¢INSERTºÍUPDATEµÄ²Ù×÷¡£Èç¹ûÄãÔÚADOdbÉÏÊǸöÐÂÊÖ£¬ÎÒ½¨ÒéÏȶÁÒ»ÏÂÉϸöÔµÄÄÇƪÎÄÕ¡£ ADOdb »¹Óкܶà¸ü¸ß¼¶µÄÌØÕ÷£¬Õâ¸öÔ£¬ÎÒÃǾÍÒ»ÆðÀ´¹Ø×¢ÆäÖеIJ¿·ÖÄÚÈÝ¡£
Êý¾Ý¿âʱ³£»á³ÉΪӦÓóÌÐòЧÂʵÍϵĻöÊס£¾¡Á¿¼õÉÙ¶ÔÊý¾Ý¿âµÄ²éѯ£¬ÊÇÌá¸ßÖ´ÐÐЧÂʵķ½·¨Ö®Ò»¡£Õ⣬ͨ³£¿ÉÒÔͨ¹ý¶ÔÕûÒ³ÄÚÈݽøÐлº´æ£¨ÓкܶàÖÖ·½·¨À´ÊµÏÖ¡£±ÈÈ磬PEAR->Cache£©,»òÕߣ¬Èç¹ûÄãÐèÒª×öÒ»ÕŶ¯Ì¬Ò³Ã棬²¢ÇÒÖ»ÏëÈòéѯָÁî±»»º´æ£¬ÄÇô£¬Äã¿ÉÒÔʹÓÃADOdb,¼òµ¥µØ½«²éѯָÁ´æÆðÀ´¡£ÔÚÄãÊÓͼͨ¹ý»º´æÀ´Ìá¸ßÄãµÄÓ¦ÓóÌÐòµÄÔã¸âÐÔÄÜ֮ǰ£¬ÎÒ½¨ÒéÄãÏÈÊÔͼȥÓÅ»¯ÄãµÄ²éѯָÁî¡£ÓÐʱºò£¬Ò»Ð©¼òµ¥µÄË÷Òý¿ÉÒԸıäÒ»ÇÐ----ÓÐÌ«¶àµÄËùνµÄרҵµÄ½â¾ö·½°¸£¬¶¼ÔÚʹÓÃÔã¸âµÄË÷Òý¡£ÔÚ±¾ÎÄÖУ¬ÄãÄÜÕÒµ½ºÜ¶àÕâÑùµÄʵÀý¡£ÏÖÔÚ£¬ÈÃÎÒÃÇÀ´¿´¿´ADOdbÊÇÈçºÎʹÄãÄܹ»¶ÔÊý¾Ý¿âµÄ²éѯ½á¹û½øÐлº´æµÄ¡£ÔÚÕâ¸öʵÀýÖÐ,ADOdb°ÑÎÒÃǵÄ×îºóµÄÒ»´Î²éѯµÄ½á¹û±£´æÔÚ/var/tmp/adodb_cacheÕâ¸ö»º´æÎļþÖУ¬²¢±£Áô10·ÖÖÓ¡£
include("$adodb_path/db_values.inc.php");
include("$adodb_path/adodb.inc.php");
$db = NewADOConnection('$database_type');
$db->Connect("$host", "$user", "$password", "employees");
$ADODB_CACHE_DIR = "/var/tmp/adodb_cache"; //Directory to store cached files
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'
';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
CacheExecute() º¯ÊýÓÐÁ½¸ö²ÎÊý: µÚÒ»¸ö²ÎÊýÊÇ»º´æÎļþ½«±»±£ÁôµÄʱ¼ä,ÒÔÃë¼Æʱ£»µÚ¶þ¸ö²ÎÊýÊÇ SQL ÉùÃ÷¡£ µÚÒ»¸ö²ÎÊýÊÇ¿ÉÑ¡ÔñµÄ (һЩ¿ª·¢Õß»òÐí»áÈÏΪËüÓ¦µ±³ÉΪµÚ¶þ¸ö²ÎÊý) £¬Èç¹ûÄãûÓÐÏÞ¶¨Ê±¼ä£¬ÄÇô£¬Ä¬ÈÏÖµÊÇ 3600 Ãë,Ò²¾ÍÊÇ1¸öСʱ¡£»º´æÎļþ±»ÃüÃûΪ adodb_*.cache£¬Äã¿ÉÒÔÔÚÎļþϵͳÖа²È«µØ½«ËûÃÇɾ³ý¡£ÄãÓ¦¸Ã¶¨ÆÚÇå³þ¹ýÆڵĻº´æÎļþ£¨ÓÃUNIXµÄ¡°crontabÍòÄêÀú¡±£¬»òÕßWINDOWSµÄ¡°¼Æ»®ÈÎÎñ¡±¡£ÒëÕß×¢£ºÍòÄêÀú²»ÖªÓÐûÓÐÒë´í£¬ÎÒûÓÐUNIX¡££©¡£Òª×¢ÒâµÄÊÇ£¬ÒªÊ¹Óûº´æ·½·¨£¬ÄãÐèÒª½«PHPµÄ²ÎÊýmagic_quotes_runtimeÉèΪoff(ÒëÕß×¢£ºÔÚphp.iniÖУ¬½«ÖµÉèΪ0)¡£Äã¿ÉÒÔ¸ù¾ÝÐèÒª£¬ÔÚÔËÐÐʱÐÞ¸ÄËüµÄÖµ£º
set_magic_quotes_runtime(0)£»
Ö»Ð轫ÉÏÊö´úÂë·Åµ½Äãµ÷ÓÃÊý¾Ý¿âµÄÖ¸Áî֮ǰ¾Í¿ÉÒÔÁË¡£Äã¿ÉÒÔÔÚÈκÎʱºò£¬Í¨¹ýµ÷ÓÃCacheFlush()£»À´Çå³ý»º´æ¡£´¦ÓÚ¶Ô°²È«µÄ¿¼ÂÇ£¬ADOdb Ò²½¨Ò齫PHPµÄ²ÎÊýregister_globals ÉèΪ0(ÔÚPHP×îаæÖУ¬ÕâÊÇĬÈÏÖµ) ¡£
¸ü¶à¹ØÓÚ²éѯ½á¹ûµÄÐÅÏ¢
¿ª·¢Õßͨ³£»á¶ÔËûÃǵÄÒªµÃµ½µÄ½á¹û²úÉúһЩÖÊÒÉ,×îͨ³£µÄÎÊÌâÊÇ£ºÓжàÉÙ¸ö¼Ç¼£¿ADOdbÄܹ»ºÜÈÝÒ×µØͨ¹ýRecordCount()À´»Ø´ðÕâ¸öÎÊÌâ¡£ RowCount()ÊÇͬÒ庯Êý¡£
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
print $rs->RecordCount() . " rows returned]"; // Display number of rows returned
»òÐíµÚ¶þ¸öÎÊÌâ»á²úÉúÔÚÐèÁ¿ÁбíÉÏ£º±»·µ»Ø×Ö¶ÎÊýÁ¿ÊǶàÉÙ£¿²»Óõ£ÐÄ£¬ADOdbÓÐFieldCount()¡£
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
print $rs->FieldCount() . " columns returned]"; // Display number of rows returned
ÏÞÖƽá¹û
ÉÏ´ÎÎÒÃÇÌÖÂÛÁËÈçºÎͨ¹ýʹÓÃÒ»¸öÊý¾Ý¿â¿âº¯ÊýʹÄãµÄÓ¦ÓóÌÐò¸ü¼ò½à£¬¸üÒ×ÓÚÒÆÖ²¡£ÔÚ´ÓMySQLתÒƵ½ InformixÖÐ , ÎÒ¾ÀúÁËÒ»´ÎÍ´¿àµÄÒÆÖ²¹ý³Ì¡£Ò»Çж¼¹é¾ÌÓÚ·ÇANSII±ê×¼µÄLIMIT×Ó¾ä( ¾ÙÀýÀ´Ëµ£¬ ÔÚMySQLÖÐÔÊÐíÏÂÁÐÖ¸ÁSELECT name FROM employee LIMIT 15),ËüÊÇÒ»¸ö·Ç³£ÓÐÓõŦÄÜ£¬¿ÉÔÚInformixÖÐÈ´²»±»Ö§³Ö¡££¨ÔÚInformixÖУ¬Ïàͬ¹¦ÄܵÄÊéдӦ¸ÃÊÇ£ºSELECT FIRST 15 name FROM employee in Informix¡££©ËüËƺõ¶ÔÄãÇÃÏìÁ˾¯ÖÓ£¬ÒªÄãÍ£Ö¹ÔÚÄãµÄ²éѯÖÐʹÓ÷DZê×¼SQLµÄÖ¸Á¶øÈ¥ÈÏÕæµØѧϰ±ê×¼µÄSQL¡£ÐÒÔ˵ÄÊÇ£¬ADOdbÓÐÒ»¸ö´¦ÀíLIMITµÄ·½·¨£ºSelectLimit()¡£
$sql = "SELECT surname, age FROM employees";
$rs = &$db->SelectLimit($sql, 10, 100); // Select 10 rows, starting at row 100
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'
';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
SelectLimit()½«SQLÉùÃ÷×÷ΪµÚÒ»²ÎÊý,µÚ¶þ¸öÊÇ·µ»ØµÄÁеÄÊýÁ¿£¬×îºóÒ»¸ö²ÎÊýÊÇÆ«ÒÆÁ¿(·µ»ØµÄµÚÒ»ÐÐ)¡£×¢ÒâÕâ¸úMySQLµÄÖ¸ÁîÖÐLIMIT×Ó¾äµÄ²ÎÊý˳ÐòÏà·´¡£SelectLimit()¶ÔÓÚ½«ËÑË÷½á¹û²¿·ÖµØÏÔʾÔÚÒ³ÃæÉÏ£¬ÊÇÊ®·ÖÓÐÓõģ¬Í¨¹ýPreviousºÍNext°´Å¥£¬¿ÉÒÔä¯ÀÀËùÓеĽá¹û¡£
²»Ö¹Ò»´ÎµØ£¬ÎÒ¿´µ½µÄ´úÂ뽫À´×ÔÊý¾Ý¿âµÄËùÓвéѯ½á¹û£¬¶¼ÓÃPHP×÷¹ýÂË´¦Àí----ɱ¼¦ÓÃÅ£µ¶! ÄãÍêÈ«¿ÉÒÔʹÓà CacheSelectLimit()º¯ÊýÀ´Öü²ØÕâÖÖÀàÐ͵Ľá¹û¡£
´¦ÀíÊÂÎñ
´¦ÀíÊÂÎñÊÇÐí¶àÓ¦ÓóÌÐòµÄÒ»¸öÖØÒªµÄÌØÕ÷¡£(¶ÔÓÚ³õѧÕßÀ´Ëµ£¬´¦ÀíÊÂÎñÊÇÄã¿ÉÒÔÌá½»Ò»´ò²éѯ²Ù×÷µÄ¹ý³Ì£¬¶øÕâЩ²éѯÊǶ¼³É¹¦ÁË£¬»¹ÊÇÓв¿·Öʧ°ÜÁË£¬¶¼ÐèÒª¿¼ÂǽøÈ¥¡£
¾µä·¶ÀýÊÇÒøÐд¦Àí¡£Ç®´ÓÄãµÄÕÊ»§×ª³ö£¬È»ºóתÈëµ½ÆäËûij¸öÈ˵ÄÕÊ»§ÖС£Ö»ÒªÆäÖÐÈÎÒâÒ»Ïîʧ°Ü£¬ÄÇô£¬ÕâÕû¸ö¹ý³Ì¶¼±ØÐë±»È϶¨ÎªÊ§°Ü¡£²»È»£¬Ç®±»»®³ö£¬¶øûÓнø¶Ô·½µÄÕÊ»§£»»òÕߣ¬Ç®Ã»Óл®³ö£¬¶Ô·½ÕÊ»§Î޶˶àÁËÒ»±ÊС·Ñ¡£)
´¦ÀíÊÂÎñ¿ÉÒÔÔÚ´úÂ뼶ÉϽøÐлú¾¯µØ¹ÜÀí¿ØÖÆ¡£ ³£Êý´íÎó¼ì²é±»ÓÃÀ´ÅжÏÊÇ·ñÒªCOMMIT£¨ÊÂÎñµÄËùÓи÷ÏÕýÈ·£¬Ö´ÐÐÕýÈ·£¬½áÊøÊÂÎñ£©»¹ÊÇROLLBACK£¨ÊÂÎñÖÐÓдíÎó£¬ËùÓиĶ¯ÐèÒª»Ö¸´ÔÀ´×´¿ö£©¡£ADOdb ÓÐһЩÄÜʹ´¦Àí²Ù×÷ÕýÈ·½øÐеÄÓÐÓõĺ¯Êý¡£ÏÂÀýÔÚij¸öÖ°Ô±µÄ½áÓà¼Ó10£¬²¢ÔÚÁíÒ»¸öÉϼõ10,Õû¸ö¹ý³ÌÓÐÁ½¸ö²éѯ£¬×÷Ϊһ¸öÍêÕûµÄÊÂÎñ¡£
$sql1 = "UPDATE employees SET balance=balance-10 WHERE id=15";
$sql2 = "UPDATE employees SET balance=balance+10 WHERE id=22";
$db->StartTrans();
$db->Execute($sql);
$db->Execute($sql2);
$db->CompleteTrans();
µ±Äãä¯ÀÀÀϵĴúÂëʱ£¬¿ÉÄܻῴµ½Ò»¸ö±È½ÏÀϵķ½·¨À´ÊµÏÖÕâÒ»ÇС£Ê¹Óà BeginTrans()¡¢CommitTrans()ºÍRollbackTrans£¨£©º¯Êýʱ£¬ÐèÒªÄã×Ô¼ºÀ´´¦Àí´íÎó¡£StartTrans£¨£©ºÍ CompleteTrans£¨£©½«»á×Ô¶¯µØ´¦Àí´íÎó,²¢Êʵ±µØ½øÐÐCOMMIT»òROLLBACK¡£µ±ÐèÒªµÄʱºò£¬Äã¿ÉÒÔͨ¹ýFailTrans()À´Ç¿ÖÆÖ´ÐÐROLLBACK¡£
µ±È»Äã¿ÉÄÜÐèÒªÖªµÀ´¦ÀíÊÇʧ°ÜÁË»¹Êdzɹ¦ÁË¡£ADOdb ÌṩÁË HasFailedTrans() À´ÊµÏÖÕâ¸ö¹¦ÄÜ¡£ Èç¹û³ö´í£¨»òÕߣ¬Äã¿ÉÒÔ½«ÕâÖÖÇé¿ö³Æ×÷FailTrans()£©£¬·µ»ØTRUE£»Èç¹û³É¹¦£¬·µ»ØFALSE¡£
$sql1 = "UPDATE employees SET balance=balance-10 WHERE id=15";
$sql2 = "UPDATE employees SET balance=balance+10 WHERE id=22";
$db->StartTrans();
$db->Execute($sql);
$db->Execute($sql2);
$db->CompleteTrans();
if ($db->HasFailedTrans()) {
// Something went wrong
}
ÖµµÃ×¢ÒâµÄÊÇ£¬ÄãµÄÊý¾Ý¿âÐèÒªÖ§³ÖÕâЩÊÂÎñº¯Êý¡£ (´ó¶àÊýµÄÊý¾Ý¿âÊÇÖ§³ÖµÄ£¬²»¹ý£¬MySQL InnoDB±íÖ§³Ö,¿É MySQL MyISAM ±í²»Ö§³Ö¡£)
ÎÒÏ£ÍûÎÒËù×öµÄÒ»ÇÐÄÜÈÃÄã¶ÔÊý¾Ý¿â¿âº¯ÊýÄÜÓм«´óµÄÐËȤ¡£»¹ÓдóÁ¿ÓÐȤµÄº¯Êý¿ÉÒÔÓÃÀ´´ÓÊý¾Ý¿â±íÖÐ×Ô¶¯Éú³ÉHTML£¬²¢ÇÒͬһ½á¹ûÓв»Í¬µÄ·½·¨À´ÊµÏÖ¡£Äã¿ÉÒÔÔÚ´ËÕÒµ½ÍêÕûµÄÊֲᡣ