Configure MongoDB with PHP 7 for XAMPP on Wind

https://www.roytuts.com/mongodb-php7-xampp-windows/

https://www.roytuts.com/mongodb-php7-xampp-windows/

Configure MongoDB with PHP 7 for XAMPP on Windows

3 Comments.MongoDB PHP xampp

Introduction

This tutorial will show you how to configure MongoDB PHP 7 for XAMPP on Windows. XAMPP is an open source, easy to install Apache distribution containing PHP, Perl and MySQL. It’s very easy to install after downloading the Windows exe inataller file.

MongoDB is one of the widely used NoSQL(Not only SQL) database in market today. MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Often we come across a situation where we end up using MongoDB with PHP in XAMPP stack.

Since MongoDB does not ship with XAMPP stack so you have to setup manually.

Prerequisites

Knowledge of PHP, any database

Configurations

Please go through the following steps in order to setup MongoDB with PHP7

Step 1. Download and install XAMPP on Windows

Step 2. Check the PHP version 7.2.1 has been installed. Create a phpinfo.php file under /xampp/htdocs and write below code into it.

<?php

echo phpinfo();

?>

Step 5. Install MongoDB. Please install MongoDB Compass as well because it is a GUI tool and will help you manage and connect to MongoDB collections easily.

Step 6. Now once the installation is complete, create folder data\db in the same drive where you installed MongoDB. For example, if you installed MongoDB in C:\Program Files\MongoDB then create C:\data\db folders as it is required in order to save the database records.

Step 7. Now connect to MongoDB server using MongoDB Compass tool or using cmd prompt. MongoDB default port is 27017 and you just click on connect button from GUI tool. Using cmd prompt you have to use below commands.

Navigate to the MongoDB bin directory, for example

cd C:\Program Files\MongoDB\server\3.6\bin

Start the server using executing below exe.

mongod.exe

Now you will see the following screen at bottom “waiting for connections on port 27017”. It means when you connect to MongoDB server from GUI or cmd prompt then you will find how many connections are there to this server.

Suppose you want to connect from cmd prompt, so use below command

Navigate to the MongoDB bin directory, for example

cd C:\Program Files\MongoDB\server\3.6\bin

Connect to the server using below exe.

mongo.exe

Step 8. Download the MongoDB PHP driver in order to establish communication between these two technologies. Choose the file 7.2 Thread Safe (TS) x86.

Step 9. Now extract the archive and copy the php_mongodb.dll file into /xampp/php/ext directory. Now open the /xampp/php/php.ini file and add the below line in order to make it work.

extension=php_mongodb.dll

Step 10. Add php to System Variables. Open computer’s Advance system settings and click on Environment Variables and add /xampp/php/ to Path under User’s variables (ex, ;/xampp/php/). Click Ok.

Step 12. Now restart the Apache server in XAMPP control panel to reflect the changes you have made to php.ini.

Step 14. Now test the MongoDB connection from PHP 7 using the below code in php-mongo.php file.

<?php

	// Configuration
	$dbhost = 'localhost';
	$dbport = '27017';

	$conn = new MongoDB\Driver\Manager("mongodb://$dbhost:$dbport");

	print_r($conn);

?>

Congratulations! You have successfully connected to the MongoDB using PHP 7.

You may also like to read MongoDB PHP 7 CRUD Example.

Thanks for reading.Tags: php7

Last updated