Environment:
uname-v#1SMP Debian3.2. $-3+DEB7U2
PHP-vphp5.4. --1~dotdeb.1(CLI) (Built:jun in the at: -: -) Copyright (c)1997- theThe PHP groupzend Engine v2.4.0, Copyright (c)1998- theZend Technologies
To install the MongoDB driver:
1, install pecl (PHP extension Module)
sudo Install Php5-dev php5-cli php-pear
2, through the PECL installation drive
sudo Install MONGO
The installation process has a hint, the default carriage return.
Edit php.ini File:
sudo VI /etc/php5/fpm/php.ini
Add a line at the end of the file:
Extension=mongo.so
Finally, restart the PHP service:
Sudo
Test
Create a new PHP file: test.php, which reads as follows:
<?PHP$user=Array( ' first_name ' = ' MongoDB ', ' last_name ' = ' Fan ', ' tags ' and ' = 'Array(' Developer ', ' user '));//Configuration$dbhost= ' localhost ';$dbname= ' Test ';//Connect to test database$m=NewMongo ("mongodb://$dbhost");$db=$m-$dbname;//Get the Users collection$c _users=$db-users;//Insert This new document into the Users collection$c _users->save ($user);?>
This is a record inserted in the Users table in the MongoDB test database. As MongoDB says, a document is inserted in the collection of database test users.
Open the test.php page in the browser and the data is inserted into the database. Go back to MongoDB to see if there is any data:
MONGO Testmongodb Shell version:2.6.3Connecting To:test>Db.users.findOne () {"_id": ObjectId ("53b79360b2c88865388b4567"), "first_name":"MongoDB", "last_name":"Fan", "Tags" : [ "Developer", "User" ]}>
Yes, you can see that the code in PHP is already in effect.
How to read MongoDB data in PHP?
Create a new test2.php file with the following contents:
<?PHP//Configuration$dbhost= ' localhost ';$dbname= ' Test ';//Connect to test database$m=NewMongo ("mongodb://$dbhost");$db=$m-$dbname;//Get the Users collection$c _users=$db-users;//Find the user with first_name ' MongoDB ' and last_name ' Fan '$user=Array( ' first_name ' = ' MongoDB ', ' last_name ' and ' Fan ');$user=$c _users->findone ($user);Var_dump($user);?>
Open test2.php in the browser to see the results.