在tinybutstrong中使用adodb

淡水河边原创,转载请保留链接。两个tinybutstrong(小强)的Database plug-ins 。一个是配合adodb使用的,一个是在ms下无需adodb的(效率貌似比adodb好一些)。

淡水河边使用access测试,access能跑,插件示例的mysql就更不是问题了。

php代码如下:

PHP代码
  1. <?php   
  2. include_once("tbs_class_php4.php");   
  3. include_once("adodb/adodb.inc.php");   
  4. include_once("tbsdb_jladodb.php"); // file that contain John Lim's ADOdb function   
  5.   
  6. $conn = NewADOConnection('ado');  
  7. //$conn = NewADOConnection('ado_access');//you can use this  
  8. //$conn = NewADOConnection('access');//you can use this  
  9. $access = realpath("Employees.mdb");  
  10. $myDSN = 'Driver={Microsoft Access Driver (*.mdb)};Dbq='. $access;   
  11. $conn->Connect($myDSN);   
  12. //$conn->debug = true;   
  13.   
  14. $tbs = new clsTinyButStrong;   
  15. $tbs->LoadTemplate("list_employee.html");   
  16.   
  17. $query = "Select * from Employees";   
  18.   
  19. $tbs->MergeBlock("blk1","adodb",$query);   
  20. //$tbs->MergeBlock("blk1",$conn,$query);//you can use this   
  21. $tbs->Show();   
  22.   
  23. ?>   

tbsdb_jladodb.php是tbs的插件。可以到官方下载 

PHP代码
  1. <?php   
  2. /*  
  3. John Lim's ADOdb functions for TinyButStrong Template Engine  
  4. Version 1.00, 2004-09-22, Skrol29  
  5. http://www.tinybutstrong.com  
  6.  
  7. Example:  
  8.     $conn = NewADOConnection('mysql');  
  9.     $conn->Connect('host', 'uid', 'pdw', 'db');  
  10.     ...   
  11.     $TBS->MergeBlock('blk1','adodb','SELECT * FROM t_examples');  
  12.  
  13. Notes:  
  14.     - Before the merge, you have to open a connection using the global variable $conn (change it if you want into the custom function).  
  15.     - The keyword 'adodb' enables TBS to call the custom functions.  
  16. */  
  17.   
  18. function tbsdb_adodb_open($source,$query) {   
  19.     global $conn ;   
  20.     $conn->SetFetchMode(ADODB_FETCH_ASSOC);    
  21.     $rs = $conn->Execute($query) ;   
  22.     return $rs ;   
  23. }   
  24.   
  25. function tbsdb_adodb_fetch($rs) {   
  26.     return $rs->FetchRow() ;   
  27. }   
  28.   
  29. function tbsdb_adodb_close($rs) {   
  30.     $rs->Close() ;   
  31. }   
  32. ?>  

html模板: 

XML/HTML代码
  1. <table border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">  
  2. <tr>  
  3.   <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Employee No.</font></b></td>  
  4.   <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Employee Name</font></b></td>  
  5.   <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Designation</font></b></td>  
  6.   <td width="25%" bgcolor="#DDDDDD"><b><font size="1" face="Verdana">Department</font></b></td>  
  7. </tr>  
  8. <row>  
  9. <tr>  
  10.   <td width="25%"><font size="1" face="Verdana">[blk1.EmployeeNO;block=row]</font></td>  
  11.   <td width="25%"><font size="1" face="Verdana">[blk1.FirstName]</font></td>  
  12.   <td width="25%"><font size="1" face="Verdana">[blk1.Positions]</font></td>  
  13.   <td width="25%"><font size="1" face="Verdana">[blk1.DepartmentDescription]</font></td>  
  14. </tr>  
  15. </row>  
  16. <row>  
  17. <tr>  
  18.   <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.EmployeeNO;block=row]</font></td>  
  19.   <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.FirstName]</font></td>  
  20.   <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.Positions]</font></td>  
  21.   <td width="25%" bgcolor="#CCCCFF"><font size="1" face="Verdana">[blk1.DepartmentDescription]</font></td>  
  22. </tr>  
  23. </row>  
  24. </table>  

设计试图:
大小: 14.72 K
尺寸: 500 x 83
浏览: 19 次
点击打开新窗口浏览全图

结果:
大小: 31.97 K
尺寸: 472 x 259
浏览: 16 次
点击打开新窗口浏览全图

如果是采用access或是mssql可以有另一种选择。大致相同。无需adpdb支持。

虽然也有adodb的字样,但是他说的是ms的adodb。

php文件如下:

PHP代码
  1. <?php   
  2. include_once("tbs_class_php4.php");   
  3. include_once("tbsdb_msadodb.php");   
  4.   
  5. $conn = new com('ADODB.Connection');    
  6. $access = realpath("Employees.mdb");   
  7. $myDSN = 'Driver={Microsoft Access Driver (*.mdb)};Dbq='$access;   
  8. $conn->Open($myDSN);   
  9.   
  10. $tbs = new clsTinyButStrong;   
  11. $tbs->LoadTemplate("list_employee.html");   
  12.   
  13. $query = "Select * from Employees";   
  14.   
  15. $tbs->MergeBlock("blk1",$conn,$query);   
  16. $tbs->Show();   
  17. ?>   

tbsdb_msadodb.php文件如下:

PHP代码
  1. <?php   
  2. /*  
  3. Ms ADODB functions for TinyButStrong Template Engine  
  4. Version 1.00, 2004-09-22, Skrol29  
  5. http://www.tinybutstrong.com  
  6.  
  7. Example:  
  8.   // SQL-Server  
  9.     $conn_str = 'PROVIDER=SQLOLEDB.1;PERSIST SECURITY INFO=FALSE;DATA SOURCE=server;INITIAL CATALOG=database;' ;   
  10.     // Ms Access   
  11.     $conn_str = 'DRIVER=Microsoft Access Driver (*.mdb);DBQ=E:\My Folder\My_Database.mdb';  
  12.     $conn = new com('ADODB.Connection');   
  13.     $conn->Open($conn_str,'uid','pwd');   
  14.     ...  
  15.     $TBS->MergeBlock('blk1',$conn,'SELECT * FROM t_examples');  
  16.  
  17. */  
  18.   
  19. function tbsdb_com_open($Source$Query) {   
  20.   
  21.     if (strlen(@$Source->ConnectionString())>0) { // Look if it's a Connection object   
  22.         if ($Source->State==1) {   
  23.             $Rs = @$Source->Execute($Query); // We use the Connection object reather than the Recordset object in order to manage errors   
  24.             if ($Source->Errors->Count>0) {   
  25.                 echo 'TinyButStrong functions for ADODB: error message when opening the query: '.htmlentities($Source->Errors[0]->Description).' <br>';  
  26.                 return false;  
  27.             } elseif ($Rs->State!=1) {  
  28.                 echo 'TinyButStrong functions for ADODB: The ADODB query doesn\'t return a RecordSet or the ResordSet is not ready. <br>';   
  29.                 return false;   
  30.             }   
  31.         } else {   
  32.             echo 'TinyButStrong functions for ADODB: Connection is not open or not ready. <br>';   
  33.             return false;   
  34.         }   
  35.     } elseif (strlen(@$Source->CursorType())>0) { // Look if it's a RecordSet object   
  36.         if ($Source->State==1) {   
  37.             $Rs = $Source;   
  38.         } else {   
  39.             echo 'TinyButStrong functions for ADODB: The specified ADODB Recordset is not open or not ready. <br>';  
  40.             return false;  
  41.         }  
  42.     } else {  
  43.         echo 'TinyButStrong functions for ADODB: The specified COM Object is not a Connection or a Recordset. <br>';  
  44.         return false;  
  45.     }  
  46.  
  47.     if ($Rs===false) return false;  
  48.       
  49.     $Fields = array();  
  50.     $iMax = $Rs->Fields->Count;  
  51.     for ($i=0;$i<$iMax;$i++) {  
  52.         $Fields[$i] = ''.$Rs->Fields[$i]->Name;  
  53.     }  
  54.  
  55.     return array('recset'=>$Rs,'fields'=>$Fields);  
  56.  
  57. }  
  58.  
  59. function tbsdb_com_fetch(&$Rs) {  
  60.     if ($Rs['recset']->EOF()) {  
  61.         $Rec = false;  
  62.     } else {  
  63.         $Rec = array();  
  64.         foreach ($Rs['fields'] as $colid=>$colname) {  
  65.             $Rec[$colname] = $Rs['recset']->Fields[$colid]->Value;  
  66.         }  
  67.         $Rs['recset']->MoveNext(); // brackets () must be there  
  68.     }  
  69.     return $Rec;  
  70. }  
  71.  
  72. function tbsdb_com_close(&$Rs) {  
  73.     $Rs['recset']->Close;   
  74. }   
  75. ?>  

 结果是一样的。如果是采用ms的平台,用这个效率应该会高一些。

附件: tbs_db_plus.rar (1.63 K, 下载次数:22)

Tags: tinybutstrong, adodb学习, adodb

« 上一篇 | 下一篇 »

只显示10条记录相关文章

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):