动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   PHP → 阅读文章

 Using COM with PHP(我就不翻译了)

作者来源: 
阅读 数 431 人次 , 2006-4-18 17:19:00 


   Using COM with PHP
     By John Lim.

PHP4 on Windows has been extended to support Microsoft's COM technology. However documentation on the COM functions is very sparse at the moment.

Here are some examples of stuff I have tried. Hope this gives you some ideas. Note that these only work when you are running PHP on a Windows Web server.


Active Data Objects (ADO) with PHP
ADO is Microsoft's database object technology. There are objects for connecting to databases, recordsets for data returned from queries, and field objects representing data elements.

Most databases do not support ADO directly. Instead most databases support 2 lower level Microsoft database technologies: ODBC and OLEDB. More databases support ODBC; but OLEDB has a reputation of being faster than ODBC.

ADO is then an API wrapper around ODBC and OLEDB.

This example opens a new ADO Connection object, opens the traditional NorthWind MS-Access database via ODBC. When we execute the SQL, a RecordSet object is returned. We then display the first 3 fields of the record-set.


<?
    $dbc = new COM("ADODB.Connection");
    $dbc->Provider = "MSDASQL";
    $dbc->Open("nwind");
    $rs = $dbc->Execute("select * from products");
    $i = 0;
    $fld0 = $rs->Fields(0);
    $fld1 = $rs->Fields(1);
    $fld2 = $rs->Fields(2);
    while (!$rs->EOF) {
        $i += 1;
        print "$fld0->value $fld1->value $fld2->value<BR>";
        $rs->MoveNext(); /*updates fld0, fld1, fld2 !*/
    }
    $rs->Close();
?>

The equivalent of PHP's print $fld0->value in VBScript is Response.Write( rs.Fields(0).value ); or more concisely Response.Write( rs(0) ) because the default collection of a recordset (rs) is Fields and default property of a Field element is value.

PHP does not support default collections and properties, so we have to write everything explicitly.


Invoking Microsoft Word with PHP
Here is another example.

<?
$word=new COM("word.application") or die("Cannot start MS Word");
print "Loaded word version ($word->Version)\n";
$word->visible = 1 ;
$word->Documents->Add();
$word->Selection->Typetext("This is a test");
?>

Word's COM object model documentation is available in the online help, but it is not in the default installation. Go to the MS Office installer, select VBScript Documentation in options and you will be ready to have some fun!


PHP COM Bugs
While testing ADO, I hit this bug with PHP 4.02. It seems that passing a COM object as a parameter to a second COM object fails.


<?
$dbc = new COM('ADODB.Connection');
$dbc->Provider = 'MSDASQL';
$dbc->Open('NWIND'); // standard sample access database northwind
$rs = new COM("ADODB.Recordset");

/* The following line fails with an INVOKE error */
/* because we are passing $dbc to $rs            */
$rs->Open('select * from product',$dbc,3); // 3 = static cursor
?>

Another bug is that the data types null and currency are not supported.

3 Jan 2001: None of the above bugs have been fixed in PHP 4.0.4. Do we have any COM C++ experts who could fix the bugs? PHP is open source after all.    
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:使用convert函数转换。。。
· 下一篇:哈,怎么跑到这里来问了?进来看
· PHP 高手之路(一)
· 记一次对php聊天室的攻击
· 求一个可移植性强一点的分页程序!!!!!!!!!!!!
· 判断字符串emailAddr是否为合法的email格式
· session 的生命周期是多长


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077