0

I'm trying to get the Windows Indexing search work directly over PHP so I can search for text inside thousands of files very quickly.

I have it working on Visual Basic with this script:

'To run this snippet, save it to a file and run it using cscript.exe from a command line. 
'Running the .vbs file with Windows Script Host may cause dialog boxes to open for each item returned from the index.

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"

objRecordSet.Open "SELECT System.ItemName FROM SYSTEMINDEX WHERE DIRECTORY='file:C:/folderIndexed' AND CONTAINS('myDemo') ORDER BY System.ItemName DESC", objConnection

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item("System.ItemName")
    objRecordset.MoveNext
Loop

Now I'm trying to port it into PHP by using the COM class as suggested here but I'm getting this error message:

com_exception
Source: Unknown
Description: Unknown

My attempt looks like so:

<?php
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';");

$recordset = new COM("ADODB.Recordset");  

$keyword = 'a';
$sql = "SELECT filename, size, path
     FROM SCOPE()
     WHERE  DIRECTORY='file:C:/folderIndexed' and CONTAINS('a')";


//-----------------> line of the error <-----------------
$recordset = $recordset->Open($sql, $conn);


foreach ($recordset as $obj) { 
    echo  $obj->Fields->Item("System.filename")->Value,  "\ n" ;  
} 

What am I doing wrong?

I'm using PHP 5.5. and I'm using extension=php_com_dotnet.dll at php.ini. The php_com_dotnet.dll file is placed at the ext folder as detailed at the extension_dir value.



Related question:
Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • i am not sure but the msdn doc says `Always use 3rd arg to COM() constructor.` – Nouphal.M Feb 11 '14 at 16:16
  • @Nouphal.M I don't see it. In fact, the two other parameters are optionals : `COM::COM ( string $module_name [, mixed $server_name [, int $codepage [, string $typelib ]]] )` and you can see the example `$obj = new COM("Application.ID")` [here](http://uk3.php.net/manual/en/class.com.php). – Alvaro Feb 11 '14 at 16:27
  • check here http://social.msdn.microsoft.com/Forums/en-US/63325b03-18f9-4506-a896-958ce55bde8a/query-windows-indexing-server-with-php?forum=sqldriverforphp – Nouphal.M Feb 11 '14 at 16:36
  • @Nouphal.M still having the exact same problem after changing it to `new COM("ADODB.Connection", NULL, CP_UTF8) ` – Alvaro Feb 11 '14 at 16:37
  • Is the error created by the creation of the com object (first line) or one of the following operations (you can check by commenting out)? – ZoolWay Feb 14 '14 at 12:03
  • Updated question. (its in this line `$recordset = $recordset->Open($sql, $conn);`) – Alvaro Feb 14 '14 at 12:07

1 Answers1

2

$recordset->open does not return a RecordSet, you should not be assigning the return value as you are doing.

Try changing:

$recordset = $recordset->Open($sql, $conn);

To

$recordset->Open($sql, $conn);

In addition, your ported code significantly deviates from structure of your VBScript version. Here is my attempt at porting your code, which works for me.

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$recordset = new COM("ADODB.Recordset");  

$conn->Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';");

// your query doesn't work for me in VBSCRIPT or PHP. this one does.
$recordset->Open("SELECT Top 5 System.ItemPathDisplay FROM SYSTEMINDEX", $conn);

if(!$recordset->EOF) $recordset->MoveFirst();
while(!$recordset->EOF) {
    echo $recordset->Fields->Item("System.ItemPathDisplay")->Value . "\n";
    $recordset->MoveNext();
}
Martin
  • 5,945
  • 7
  • 50
  • 77
  • Thanks @Martin!! The Main problem was on the query, I was not using `SYSTEMINDEX` and neither valid `fields` for the select, now I'm using `"SELECT System.ItemName, System.DateModified FROM SYSTEMINDEX WHERE DIRECTORY='file:C:/xmls/folder1' AND CONTAINS('a')"` plus the change you mentioned and your looping through the records. – Alvaro Feb 17 '14 at 14:46
  • In case you know more about the topic, I will open a new question regarding the "limit" or "maxRecords" for the query under the same tags. :) – Alvaro Feb 17 '14 at 14:48
  • Here it is the related issue: http://stackoverflow.com/questions/21832267/set-a-limit-maxrecords-property-for-windows-indexing-search-through-php-com – Alvaro Feb 17 '14 at 14:57
  • If there are no resutls I get this error in the line where `$recordSet->MoveFirst()` is: `Source: ADODB.Recordset
    Description: Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.`
    – Alvaro Feb 18 '14 at 18:08
  • I have edited my answer to add an additional EOF check. – Martin Feb 19 '14 at 13:46