In this post, we'll discuss getting PHP to run with MongoDB on Windows. I'll assume you already have MongoDB up and running. You might need a different copy of PHP, though.
I made a folder called "PHP_5_4_12" first. Then, I went to http://windows.php.net/download/ and downloaded the "VC9 x86 Thread Safe" Zip file. I unpacked the Zip to C:\PHP_5_4_12. Then I went to the IIS manager through Control Panel. I went to Handler Mappings, and clicked on "Add Module Mapping". I entered "*.php" (minus the quotes) for the Request path, selected FastCgiModule, and named it PHP. For the executable, I entered "C:\PHP_5_4_12\php-cgi.exe" minus the quotes. For the request restrictions, I selected "File Or Folder". I agreed to have IIS add this handler to the FastCGI list.
The next step was getting the MongoDB PHP drivers. I downloaded the latest version (it was 1.3.2 RC 1when I did this) from https://github.com/mongodb/mongo-php-driver/downloads . I then unpacked the Zip and copied the php_mongo-1.3.2RC1-5.4-vc9.dll DLL to C:\PHP_5_4_12\ext. Also, I right-clicked on the new copy, went into Properties and unblocked the file.
The next step was configuring PHP. I went into the php.ini (copy one of the candidate INIs and rename the copy if you don't have a file with that specific name). I removed the semicolon from the
;extension_dir = "ext"
entry. That tells PHP where to find extension DLLs. The last change I had to make was go into the extension list and add the following to the bottom of the list prior to the MIBS list:
extension=php_mongo-1.3.2RC1-5.4-vc9.dll
Use the exact name of the DLL file you copied into the "ext" directory. I saved my changes.
In my case, I have a MongoDB database called "mvcmongo3" with a collection of names called "names". To test my work, I created a script file called "mongo2.php", which consisted of:
<?php
class SearchName {
public $firstName;
public $lastName;
public function SearchName($first, $last)
{
$this->firstName = $first;
$this->lastName = $last;
}
}
class Name extends SearchName {
public $_id;
# public $firstName;
# public $lastName;
public function Name($obj)
{
$this->_id = $obj['_id'];
$this->firstName = $obj['firstName'];
$this->lastName = $obj['lastName'];
}
}
$m = new MongoClient();
$db = $m->mvcmongo3;
$col = $db->names;
$cursor = $col->find();
foreach ($cursor as $obj) {
$nameObject = new Name($obj);
print "$nameObject->_id : $nameObject->firstName $nameObject->lastName <br />";
}
echo "Found " . $cursor->count() . " names.<br />";
$name = new SearchName("Ross", "Albertson");
print "Found " . $col->find($name)->count() . " matches <br />";
$cursor2 = $col->find($name);
foreach ($cursor2 as $obj) {
$nameObject = new Name($obj);
print "$nameObject->_id : $nameObject->firstName $nameObject->lastName <br />";
}
?>
Running the script in C:\inetpub\wwwroot, I got the following results....
513772c6e037b778819b159c : Ross Albertson
51377310e037b778819b159d : David Albertson
5137732fe037b778819b159e : Ross Babcock Jr.
513774d2e037b778819b159f : David Acker
51377568e037b778819b15a0 : David Johnson
513777e4e037b778819b15a1 : Sara Werckle
Found 6 names.
Found 1 matches 513772c6e037b778819b159c : Ross Albertson
$col->find() retrieved a list of all of the names; $col->find($name) retrieved all the documents with a first name of "Ross" and a last name of "Albertson". The funny-looking string are the document IDs. I think $col->save($name); will store the name into that MongoDB collection, given my naming convention. Given that, try experimenting with php_mongo.
Nice post. Very succinct. if you want to avoid extra typing, you could format the commands to be typed with a line break, indent and change of font, just as you did with system messages.
ReplyDelete