V
V
Victoria2016-04-05 18:49:58
PHP
Victoria, 2016-04-05 18:49:58

Error creating instance of MongoClient class, what's the problem?

Here is such a function for saving in MongoDB:

function save($data)
{
    $m = new MongoClient();
    $con = $m->selectDB(DB_NAME)->selectCollection(COLLECTION_NAME);
    $con->insert($data);
    $m->close();
}

$date = array("name" => "Vika");

save($date);

When I run the following error:
Fatal error: in C:\xampp\htdocs\parsin\save.php on line 8 line 8
I have $m = new MongoClient();
What could be the problem? Mongo seems to be installed correctly, the phpinfo() function gives all the information about it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victoria, 2016-04-11
@vikusechk

The problem was solved! I'll tell you how:
1) Only "Fatal error ..." was displayed in the browser, it was difficult to understand what exactly the error was. I found the logs in the php folder and found this error by date and time. Everything was explained there. The problem was that when connecting to a port, it simply does not find a running database, although it starts automatically for me.
2) I started checking the work of mongo itself, also went into the logs, and saw that it tries to start every time the computer is turned on, but an exception occurs. I'll still deal with this, but as soon as I manually restarted mongo, everything worked)

D
DieZz, 2016-04-07
@DieZz

If your method is in a namespace other than the global one, write like this:
$m = new \MongoClient();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question