Is it ok to define PDO db connection in function?

grab

I am thinking, is it safe and OK to use db connection in function to return it anywhere in the project? If I want to reuse this project to build another one, I can change db connection only in this function and I don't need to go all over the script searching and changing connection parameter.

function connect_db(){
$db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', '');
return $db;
}

Now I can call it anywhere functions.php file is required once, by returning

$db = connect_db();

and then whatever statement follows.

Is it ok and safe?

And how to close connection like this at the end of the page?

 $db = NULL;

apparently won't work.

Benny Hill

Yes, it is safe and ok to have a single place that creates a connection to your database. I would change your function just a bit though:

<?php
function connect_db(){
    try{
        return new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND =>'SET NAMES utf8'));
    }catch (PDOException $e){
        echo '<p>Error connecting to database! ' . $e->getMessage() . '</p>';
        die();
    }
}
?>

You can read more about the PDO constructor in the documentation.

And to close the PDO connection created this way you do indeed just:

<?php
$pdo = NULL;
?>

Please note that there is really a lot more to be said on this subject. Ideally you would have a class that handles the creation of your PDO object(s). You might want to take a look at this excellent answer from Gordon as a place to start.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Manually define db connection in laravel

From Dev

Passing a db connection in function

From Dev

error handling if pdo db connection is not present?

From Dev

Is it good or bad to store the DB connection details with define()?

From Dev

php: mysql db connection ok but can't perform query

From Dev

PDO Connection Error Within Recursive PHP Function

From Dev

PDO Connection: Call to a member function prepare() on null

From Dev

php pdo function connect to DB and do query

From Dev

Is it ok to define static inline function with same name in different files?

From Dev

datatables in pdo style - inject db connection into class causing errors

From Dev

DB2 how to specify connection charset using PDO

From Dev

DB class having pdo, on inheriting should not re-initiate connection

From Dev

how to set the connection with a schema in a Postgresql DB with PDO PHP

From Java

Error: db.sequelize.define is not a function in NodeJS

From Dev

PHP Define a new PDO object inside class (outside function)

From Dev

PHP, DB connection in Function. Is Separate Connection Mandatory?

From Dev

PHPUnit reuse PDO connection

From Dev

PHP PDO Class connection

From Dev

Using PDO connection method

From Dev

SQLSRV PDO connection with domain

From Dev

Undefined variable PDO Connection

From Dev

Error on PDO SSL connection

From Dev

PDO Connection and abstract class

From Dev

Cassandra PDO connection PHP

From Dev

Close connection PDO

From Dev

PDO Connection fails

From Dev

Destroy PDO connection?

From Dev

define db value while creating connection with redis into nginx.conf file while using Openresty

From Dev

What are the advantages to passing a global DB connection into each function of a model?

Related Related

HotTag

Archive