Use MongoDB and php in combination [windows]

Source: Internet
Author: User
After learning how to use and deploy MongoDB, I tried how to apply mongodb to a php program. 1. Prepare mongodb for the preparatory work, and deploy it and ensure that the service is running properly. For beginners, you can refer to the Code farming tutorial for detailed and practical instructions. Reference Method and Tutorial: www. cnblogs.

After learning how to use and deploy MongoDB, I tried how to apply mongodb to a php program. 1. Prepare mongodb for the preparatory work, and deploy it and ensure that the service is running properly. For beginners, you can refer to the Code farming tutorial for detailed and practical instructions. Reference Method and Tutorial: http://www.cnblogs.

After learning howUseAnd deploy MongoDB. I tried how to apply mongodb to a php program.

1. Preparations

First, you have to prepare mongodb and deploy the service as required and the service is running properly.

For beginners, you can refer to the 'code nount' tutorial, which is very detailed and practical.

Reference Methods and Tutorials: http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html

D: \ mongodb \ bin> net start MongoDB

Php and apache environment preparation

If it is relatively lazy, you can go to XAMPP and next integrated installation package, including php, apache, and mysql, and the Environment is already integrated. You only need to put the php program under htdocs/for access.

XAMPP address: http://www.apachefriends.org/zh_cn/xampp-windows.html (complete installation and Application)

Mongodb drivers is a dynamic link library file.

Address: https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

2. Integration of mongodb and php

(1) Find the ext directory in xampp:

For example, I installed it on 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 the file in the editor and find; Windows Extensions

Add a configuration line (about: 1033 rows) to the end of the block)

Extension = php_mongo-1.4.5-5.5-vc11.dll

Save and restart the Apache service.

3. debugging and calling

Write test. php under xampp/htdocs,

 

Run test. php In the browser. If you can find the configuration item,

Mongo

MongoDB Support Enabled
Version 1.4.5
SSL Support Enabled
Streams Support Enabled

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

It turns out that mongodb has been integrated with php.

You can write a small program to test it:

 UseIs port 2222. // Mongodb: // $ server-path or $ ip-address: $ port $ client = new container client ("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 displayed in the browser is: NULL $ array = array ('name' => 'test', 'age' => 25, 'address' => array ('vince '=> 'guangdong', 'city' => 'shenzhen ', 'zone '=> array ('area' => 'nanshance', 'local' => 'shanghai '))); $ 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 (upload id) #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" }}// view the database directly by the mongo client or d: \ mongodb \ bin> mongo 127.0.0.1: 2222> use test> db. person. find ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.