ΜongoDB PHP, Error: Call to undefined method MongoDB\Driver\Manager::getEncryptedFieldsMap() - php

I am trying to create a collection on my MongoDB database using PHP but I am getting the following error
*Uncaught Error: Call to undefined method MongoDB\Driver\Manager::getEncryptedFieldsMap() *
I have downloaded and added the MongoDB PHP Driver, via the php_mongodb.ddl file (have made sure to download the correct driver version for my php version), and have added the MongoDB PHP Library via Composer using the composer require jenssegers/mongodb --ignore-platform-reqs, all with no errors.
My code is the following:
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://127.0.0.1/");
echo "connected succ <br><br>";
$db = $client->webVacSer;
echo "db sellected successfully <br><br>";
$collection = $db->createCollection("customers");
echo "collection created successfully <br><br>";
?>
What is wrong?

Related

Uncaught Error: Class 'WindowsAzure\Common\ServicesBuilder' not found- MS Azure | PHP

I have created the composer.json file in my root folder where my index.php file is presents, with the following code in it:
{
"require": {
"microsoft/windowsazure": "^0.5"
}
}
and on downloading composer.phar, I have installed it using:
php composer.phar install
I'm trying to create a table and add entities to it in php. I use the command
use WindowsAzure\Common\ServicesBuilder;
$connectionString = 'DefaultEndpointsProtocol=https;AccountName=******;AccountKey=***/***************************/******************/**********************************==';
$tableRestProxy = ServicesBuilder::getInstance()->createTableService($connectionString);
try {
// Create table.
$tableRestProxy->createTable("mytable");
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
When I run this on local host on my Ubuntu, I get an error saying-
Uncaught Error: Class 'WindowsAzure\Common\ServicesBuilder' not found in /home/my_folder/php-docs-hello-world-master/index.php:30
If I add
require_once 'vendor/autoload.php';
before defining my $connectionString, then my error changes to:
/index.php - Uncaught RuntimeException: Error creating resource: [message] fopen(https://eyesav.table.core.windows.net/Tables): failed to open stream: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
Can someone help me figure out this issue, if it is with the installation of my composer, or my connectionString, or something else?
Thanks in advance :)
Can someone help me figure out this issue, if it is with the installation of my composer, or my connectionString, or something else?
If I use the code you mentioned, I also could reproduce the issue you mentioned.
Please have a try to use following code to create the table client. It works for me.
use MicrosoftAzure\Storage\Table\TableRestProxy;
use MicrosoftAzure\Storage\Common\ServiceException;
$tableClient = TableRestProxy::createTableService($connectionString);
The following is the demo code from azure official document.
<?php
require_once "vendor/autoload.php";
use MicrosoftAzure\Storage\Table\TableRestProxy;
use MicrosoftAzure\Storage\Common\ServiceException;
$connectionString = 'DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=xxxxxxx;';
$tableClient = TableRestProxy::createTableService($connectionString);
try {
$tableClient->createTable("mytable");
}
catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}

Cannot connect mongoDB and PHP

I am successfully running mongoDB v3.2.10 in terminal on macOS 10.11.6 and MAMP server running php 7.0.8.
When I try to reach mongo via php code like:
<?php
// connect to mongodb
$m = new MongoClient();
?>
at url: http://localhost:8888/Mongo/login.php
I get:
This site can’t be reached
localhost refused to connect.
When I reach mongo on the native port
at url: http://localhost:27017/Mongo/login.php
I get:
It looks like you are trying to access MongoDB over HTTP on the native
driver port.
Any idea what am I missing here?
Looks like you are using the built-in PHP Mongo DB clients. There is a new MongoDB driver that replace that. You will have to install it.
http://php.net/manual/en/set.mongodb.php
The newer MongoDB extension replaces the built-in PHP Mongo DB client. You'll also want to use the composer PHPLIB MongoDB client from here: http://php.net/manual/en/mongodb.tutorial.library.php
use MongoDB\Client;
try {
$mongoDbClient = new Client('mongodb://localhost:27017');
} catch (Exception $error) {
echo $error->getMessage(); die(1);
}

Class 'MongoClient' not found in xampp

I want to connect mongodb with php.
PHP version : 5.6
Mongodb:
System : 64 bit
Xampp : v3.2.1
Window : 7
I have added php_mongo.dll (as per system 64 bit) into php.ini file and copied that dll into php/ext folder.when i am trying to connect, i am getting following message.
Fatal error: Class 'MongoClient' not found in D:\xampp\htdocs\
connection file
<?php
$MongoDBConnection = new MongoClient();
$DB = $MongoDBConnection->selectDB('SimpleCrud');
$pessoas = $DB->pessoas;
?>
To use MongoDB with PHP, you need to use MongoDB PHP driver. CLICKHERE to download the driver. Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory ("C:\xampp\php\ext" by default) and add the following line to your php.ini file.
extension = php_mongo.dll
Following is the code snippet to connect to the database :
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->myTestdb; // myTestdb-> Your Database Name
echo "Database myTestdbselected";
?>
When the program is executed, it will produce the following result −
Connection to database successfully
Database myTestdb selected

Class 'MongoClient' not found, while php connecting to mongodb in ubuntu

I installed mongodb version 3.0.7 in xampp, and installed php driver also. below is the php code to connect mongodb.
<?php
// connect
$m = new MongoClient();
// select a database
$db = $m->test;
?>
While I run this code in my localhost I am getting this error
Fatal error: Class 'MongoClient' not found in /opt/lampp/htdocs/mongo.php on line 3
<?php
// connect
$m = new MongoClient();
// select a database
$db = $m->test;
?>
This above code working fine, In xampp server you must include "extension=mongo.so" in php.ini file at usr/lib/php5/20121212/php.ini with same extension. After all setting restart your server.

Mongodb installation

Mongodb installed in linux CentOS server (ssh) with steps mentioned in this url
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
Started MongoDB server using the command and the status is ok.
sudo service mongod start
When connecting mongodb with PHP (Yii app), it shows error like this.
include(MongoClient.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
PHP code
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->health;
echo "Database mydb selected";
$collection = $db->medical;
echo "Collection selected succsessfully";
?>
Yii2 provide class yii\mongodb\Connection for mongodb connection.
Check below link.
http://www.yiiframework.com/doc-2.0/yii-mongodb-connection.html
MongoDb Extension for Yii 2
http://www.yiiframework.com/doc-2.0/ext-mongodb-index.html
It will resolve your issue.

Categories