Skip to content

Instantly share code, notes, and snippets.

@banker
Created January 25, 2011 22:14
Show Gist options
  • Save banker/795791 to your computer and use it in GitHub Desktop.
Save banker/795791 to your computer and use it in GitHub Desktop.
Sample PHP program using MongoDB and the official PHP driver.
<?php
// connect
$m = new Mongo();
// select a database
$db = $m->training;
$coll = $db->messages;
for ($i = 0; $i < 10; $i = $i + 1) {
$coll->insert( array( "text" => "Hello World", "n" => $i ) );
}
$cursor = $coll->find();
foreach ($cursor as $obj) {
print $obj['_id'] . "\n";
print print_r( $obj ) . "\n";
}
print "\nCount " . $coll->count() . "\n";
$coll->remove();
print "\nCount " . print_r( $db->command( array( "count" => "messages" ) )) . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment