PHP - PDO connection

Hello experts,

this is not working. The function does not return the PDO object -> btw. could it be done actually? Or how to do it right?

function databaseConnect($host, $dbname, $user, $pass) {
    try
    {
        $pdo = new PDO('mysql:host=' . $host . '; dbname=' . $dbname, $user, $pass);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec('SET NAMES "utf8"');
        return $pdo;
    }
    catch (PDOException $e)
    {
        $error = 'Unable to connect to the database server.';
        echo $error;
        //include 'error.html.php';
        exit();
    }
}

Open in new window

LVL 1
Fajer39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This article shows how to use PDO, including how to get meaningful error messages when things go awry.  Please try the examples here, and post back if you still have questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Fajer39Author Commented:
Well, I know how to use PDO object and that for a proper error I have to concatenate $e variable to $error...  but that's not my Q- btw. I'm calling that databaseConnect function in my index.php...

I'm parsing strings (the same one which works without the use of function and connection is establish)...the idea here is just to create a helper.inc.php and database connection is definitely one of the most used...I have to know why it's not returning $pdo object.

*sure I can create db.inc.php without the function just for every project with the specific credentials which will work just fine...but I'm just curious generally how things work in PHP so that's why I asked.
0
Ray PaseurCommented:
If you're new to PHP you will find no shortage of simply terrible and incompetent examples littering the internet.  This article can help you find the good examples and dependable learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

I'll try to set up an example for you using your code as a starting point.  Just curious, how do you know that it is not returning the PDO object?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Ray PaseurCommented:
See "Using UTF-8 with PDO" in this article:
http://iconoun.com/articles/collisions/

This seems to work OK.
<?php // demo/temp_fajer39.php

/**
 * http://www.experts-exchange.com/questions/28693112/PHP-PDO-connection.html#a40856727
 */
error_reporting(E_ALL);

function databaseConnect($host, $dbname, $user, $pass, $mbie="utf-8")
{
    // SWITCH PHP INTERNAL ENCODING
    mb_internal_encoding($mbie);
    mb_http_output($mbie);

    // CONSTRUCT A DSN USING HOST, DATABASE, CHARACTER SET
    $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";

    try
    {
        $pdo = new PDO($dsn, $user, $pass);
    }
    catch (PDOException $exc)
    {
        var_dump($exc);
        trigger_error('NO PDO Connection', E_USER_WARNING);
        return FALSE;
    }

    // SET PDO TO USE UTF-8
    $pdo->setAttribute( PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8mb4');

    // SET PDO TO TELL US ABOUT WARNINGS OR TO THROW EXCEPTIONS
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    return $pdo;
}

/**
 * PUT YOUR OWN DATABASE CREDENTIALS INTO THE SCRIPT HERE
 */
$x = DatabaseConnect($host, $dbname, $user, $pass);
var_dump($x);

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fajer39Author Commented:
Thank You very much Ray, that's awesome comment with nice working script based on my example. I appreciate Your effort.

I've already voted for Your article. Thank You.

How do I know? Well in index.php I called a function and then I tested with echo isset($pdo)  but I did a silly misstake...I didn't assign DatabaseConnect($host, $dbname, $user, $pass); to some variable...
0
Ray PaseurCommented:
Thanks for the points and thanks for using E-E.  That said, you might want to look a little more into the idea of object-oriented programming.  A good design might extend the PDO object, rather than returning it from a function call.  That way you could keep all of the advantages of OOP in your application, like testability, encapsulation, etc.  Just a thought.

Best of luck with your project, ~Ray
0
Fajer39Author Commented:
Yes, you're right. I was just playing with some basic CMS, which I didn't do in OOP style but the next step for me is gonna be to re-write that project in OOP :-)

Thank You, Mark.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.