Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:59+00:00 2026-06-13T22:11:59+00:00

There is a little pdo problem, that I have been working on for a

  • 0

There is a little pdo problem, that I have been working on for a while now. Since I don’t know what is wrong here, I thought about taking it to this list. Maybe some of you know more…

I have a website with a login that checks a user and a password against a mysql driven database. When the pdo connection is made in the same file all works fine, one can log in, without any problems. Just as it is supposed to work…

However, when moving the database connection part to a seperate function, which I include from another file, pdo fails on me, and gives me:

SQLSTATE[28000] [1045] Access denied for user ‘…’@’…’ (using
password: NO)
Fatal error: Call to a member function prepare() on a non-object in /…/…/… on line 41

For the sake of clarity, here is the code:

Version 1:

This works:

<?php
    require "./vars_and_functions.php";

    /* open database connection */
    try {
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);

    /* query */
    $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
    $q = $pdo->prepare($query);
    $q->execute(array($u_name, $p_word_md5));
    $result = $q->rowCount();

    if($result == 1) { /* we have a match */                       
    /* close the database connection */
               $pdo = null;  

                /* and redirect */
                header("...");

    } /* if */
    else { /* wrong credentials */
        /* close the database connection */                
                $pdo = null;

                /* and go back to the login page */ 
        header("...");
    } /* else */
        } /* try */ 
    catch(PDOException $e) {  
        echo $e->getMessage();  
    } /* catch */
?>

Here is version 2

This does not work:

<?php
        require "./vars_and_functions.php";

        /* open database connection */
        $pdo = database_connection();



    /* query */
            $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
            $q = $pdo->prepare($query);
            $q->execute(array($u_name, $p_word_md5));
            $result = $q->rowCount();

        if($result == 1) { /* we have a match */                       
               /* close the database connection */
                   $pdo = null;  

                    /* and redirect */
                    header("...");

        } /* if */
        else { /* wrong credentials */
            /* close the database connection */                
                    $pdo = null;

                    /* and go back to the login page */ 
            header("...");
        } /* else */
            } /* try */ 
    catch(PDOException $e) {  
        echo $e->getMessage();  
    } /* catch */
?>

My includefile vars_and_functions.php looks like this:

$db_host = "...";       
$db_name = "...";           
$db_user = "...";       
$db_pass = "...";       

function database_connection() {
    try {
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
}  
catch(PDOException $e) {  
    echo $e->getMessage();  
}  

return $pdo;
}

The only real difference to my mind is that here, the pdo connection is made via a function call, whereas the function sits in the include file vars_and_functions.php.

What’s wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:12:00+00:00Added an answer on June 13, 2026 at 10:12 pm

    Your function database_connection() doesn’t receive the connection variables in the correct scope, so they are not set when the connection is attempted and therefore passed as NULL, and PDO defaults the connection host to localhost.

    Pass them as parameters to the function:

    // Defined at global scope...
    $db_host = "...";       
    $db_name = "...";           
    $db_user = "...";       
    $db_pass = "...";       
    
    // Pass the 4 variables as parameters to the function, since they were defined at global
    // scope.
    function database_connection($db_host, $db_name, $db_user, $db_pass) {
        try {
        $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    }  
    
    // Called as:
    $pdo = database_connection($db_host, $db_name, $db_user, $db_pass);
    

    If you are only using those variables inside the connection function and don’t need them elsewhere, consider defining them in scope of the function, which saves you passing them as parameters.

    function database_connection() {
        // Only needed here, so define in function scope
        $db_host = "...";       
        $db_name = "...";           
        $db_user = "...";       
        $db_pass = "...";       
    
        try {
        $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    }  
    

    The final and often least desirable option is to define the variables at global scope as you have done, but access them via $GLOBALS[] (or the global keyword) in the function:

    function database_connection() {
        try {
        $pdo = new PDO("mysql:host={$GLOBALS['db_host']};dbname={$GLOBALS['db_name']}", $GLOBALS['db_user'], $GLOBALS['db_pass']);
    } 
    

    Note that if you are developing with error_reporting turned on and display_errors as you should be, you would see notices about undefined variables.

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working with pthreads a fair bit recently and there's one little thing
So I have this problem when I run a script with PHP PDO that
I'm working on a quick page intended for mobile browsers. While there is little
Much have been written about the benefits of using PDO::prepare , but little has
I'm new to using PDO and have got a little bit stuck- is there
There is very little code out there that is in VB, and i'm getting
I did a little bit of Web Programming here and there but I never
Does anyone know if there is a viable library for PDO database access for
Even though the API has been open since Mac OS X Leopard, there's surprisingly,
I know of imperative and functional programming but there was little I could find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.