M
M
Max Ba2018-02-26 10:31:49
PHP
Max Ba, 2018-02-26 10:31:49

How to connect to mysql as needed?

There is a project with old mysql. Almost all parts have been replaced by pdo, but in some places on the site there is still old code, which will be replaced over time. Now you have to use the database using the DB class and the old permanent connection.
How can I make this permanently old connection open when needed?
Looking for something like

if(session_id() == '') {
  session_start();
}

mysql only.
If it is impossible, write simply that it is impossible and that's it. If possible, how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
synapse_people, 2018-02-26
@synapse_people

What's the problem with doing

if (!$db)
$db = new \PDO(...

?
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question