M
M
MR.TOSTER Gipard Valerievich2016-06-26 11:01:29
PHP
MR.TOSTER Gipard Valerievich, 2016-06-26 11:01:29

What is the best way to connect to database in php mongodb driver?

Does the connection to the database stay on after some query is executed?
Or is it closed and you need to open a new one for each request?
The connection design in the new version of php is inconvenient for stuffing it into a function, and this makes you think that you need to put the entire request with the connection into the function, but then the connection will be opened every time with a request, and if there are 10 of them in the script, it’s already scary like that.
Tell those who know about these features. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2016-06-26
@taliban

Everything works the same way as all databases, if you have an idea about how any databases work, use this knowledge for monga.

E
Eugene, 2016-06-26
@Nc_Soft

If I need to do something quickly, I do it like this

function mongo()
{
    static $mongo;
    if (is_null($mongo)) {
        $mongo = new MongoClient('mongodb://127.0.0.1:27017');
    }
    return $mongo;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question