By learning how to use and deploy MongoDB, I tried how to apply mongodb to php programs.
1. Preparatory work
First of all, mongodb must be prepared, deployed according to relevant methods, and the service can run normally.
For beginners, you can refer to the tutorial of “Code Farmer”, which is very detailed and practical.
the
d:\mongodb\bin>net start MongoDB
Preparation of php and apache environment
If you are lazy, you can go to XAMPP to download an integrated installation package, including php/apache/mysql, etc., and the environment is already integrated. You only need to put the php program under htdocs/ to access it.
XAMPP address: http://www.apachefriends.org/zh_cn/xampp-windows.html (complete installation and application)
the
The drivers of mongodb are some dynamic link library files.
the
Address: https://s3.amazonaws.com/drivers.mongodb.org/php/index.html
the
2. The integration of mongodb and php
(1). Find the ext directory in xampp:
For example, mine is installed on the D drive: D:\xampp\php\ext
(2). Copy the downloaded dynamic link library file to the above directory D:\xampp\php\ext
“
php_mongo-1.4.5-5.5-vc11.dll copy to D:\xampp\php\ext
(3). Modify the configuration of php.ini
Location: D:\xampp\php.ini
Open this file with an editor and find ;
Windows Extensions
“
And add a line of configuration at the end of this block (approximately: the position of line 1033)
“
extension=php_mongo-1.4.5-5.5-vc11.dll
Save and restart the Apache service.
3. Debugging and calling
Write a test.php under xampp/htdocs/,
the
the
the
the
Run this test.php on the browser, if you can find the configuration item,
the
mongo
the
MongoDB Support | enabled |
---|---|
Version | 1.4.5 |
SSL Support | enabled |
Streams Support | enabled |
the
Directive | Local Value | Master Value |
---|---|---|
mongo.allow_empty_keys | 0 | 0 |
mongo.chunk_size | 262144 | 262144 |
mongo.cmd | $ | $ |
mongo.default_host | localhost | localhost |
mongo.default_port | 27017 | 27017 |
mongo.is_master_interval | 15 | 15 |
mongo.long_as_object | 0 | 0 |
mongo.native_long | 0 | 0 |
mongo.ping_interval | 5 | 5 |
the
It proves that mongodb has been integrated with php.
the
You can write a small program to test it:
the
<?php
//My mongodb is installed on this machine and uses port 2222.
//mongodb://$server-path or $ip-address:$port
$client = new MongoClient(“mongodb://127.0.0.1:2222”);
//select test database and select person records.
$collection = $client->test->person;
$result = $collection->findOne();
var_dump($result);
//NULL
//The result seen from the browser is: NULL
$array = array(
‘name’=>’test’,
‘age’=>25,
‘address’=>array(
‘province’=>’guangdong’,
‘city’=>’shenzhen’,
‘zone’=>array(
‘area’=>’nanshan’,
‘local’=>’xilili’)
)
);
$ret = $collection->insert($array);
var_dump($ret);
//The result is:
array(4) {
[“n”]=>
int(0)
[“connectionId”]=>
int(2)
[“err”]=>
NULL
[“ok”]=>
float(1)
}
$res = $collection->findOne();
var_dump($res);
//The result is:
array(4) {
[“_id”]=>
object(MongoId)#7 (1) {
[“$id”]=>
string(24) “52d605b0a6b9f6581000002b”
}
[“name”]=>
string(4) “test”
[“age”]=>
int(25)
[“address”]=>
array(3) {
[“province”]=>
string(9) “guangdong”
[“city”]=>
string(8) “shenzhen”
[“zone”]=>
array(2) {
[“area”]=>
string(7) “nanshan”
[“local”]=>
string(6) “xilili”
}
}
}
//You can also see the db directly from the mongo client
d:\mongodb\bin>mongo 127.0.0.1:2222
>use test
>db.person.find()