MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Problem with mongodb PHP library

I am using PHP 7.3.16 with mongodb driver 1.7.4 , mongodb Library 1,6 connected with composer.

simple connect to DВ

$mongo = new MongoDB\Client(“mongodb://:****@serverIP:27017”);

And I got error :

" Parse error : syntax error, unexpected ‘function’ (T_FUNCTION), expecting identifier (T_STRING) or \ (T_NS_SEPARATOR) in /usr/local/bin/vendor/mongodb/mongodb/src/functions.php on line 31"

line 31 is : “use function end;”

I can not understand the possible reason

Hi @Vladimir_Anokhin, welcome!

Looking at the connection URI, looks like you may have a special characters in the password part. The uri is a URL, hence any special characters in its components need to be URL encoded according to RFC 3986. You can try to encode the user/password of the URI, for example:

$uri = sprintf("mongodb://%s:%s@server:27017/foo", rawurlencode("user"), rawurlencode("pwd"));
$client = new MongoDB\Client($uri);

Regards,
Wan.