hannebaumsaway
hannebaumsaway

Reputation: 2734

PDO object not in scope of a function

OK, I see some similar questions to mine, but their examples all use PHP classes...mine does not. Maybe that's the problem? I shouldn't need classes because my site is exceedingly simple at this point in time.

Anyway, I'm trying to use PDO to connect to a MySQL db. I connect to the db fine in a file called config.php, and include this file in index.php with require_once().

I can successfully query the db from another file called process.php, but the problem is within a function within that file; it seems my DBO object is out of scope within that function.

Here are the relevant code snippets:

index.php

require_once('./lib/config.php');

config.php

// tested and connects fine    
$pdo = new PDO('mysql:host=' . $hostname . ';dbname=' . $dbname, $username, $password, array(
    PDO::ATTR_PERSISTENT => true
));

process.php

<?php
...
// can call $pdo fine in this file outside of functions
...

function authenticate($u, $p) {
    // can't call $pdo in here, error says $pdo is non-object
    $que = $pdo->query('select user_id, user_pass from users where user_name = \'' . $u . '\' limit 1');
    ...
}

?>

By the way, I'm using PDO because I was having similar trouble with mysqli, and am trying to get away from mysql, which is apparently depreciated and discouraged.

EDIT: I should have clarified first based on the number of responses I got on this matter: I did try to pass $pdo in as a param to the function, with no luck or change in the error message.

SOLUTION: OK, apparently the problem was that I needed to add require_once('config.php') in my process.php file as well. Not sure why (wouldn't it already be included when index.php was run first?). Then I was able to successfully pass $pdo in as a param to my function, and voila.

Upvotes: 2

Views: 3148

Answers (3)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

That's pretty basic PHP stuff. Variables inside functions are local variables unless you use the global keyword to load them. I suppose you want this:

function authenticate(PDO $pdo, $u, $p) {
    $que = $pdo->query('select user_id, user_pass from users where user_name = \'' . $u . '\' limit 1');
    //...
}

Edit: If PHP claims that $pdo is not an object, it's not an object, so it doesn't really matter how it's passed to the function. Inspect the variable right before you call authenticate():

var_dump($pdo);

Without the relevant code there's no way to say why. (Assuming it's true that new PDO succeeds.)

Upvotes: 4

RMcLeod
RMcLeod

Reputation: 2581

because $pdo has been declared outside of the function authenticate it isn't available inside it. You need to either pass $pdo in

function authenticate($u, $p, $pdo) {
    $que = $pdo->query('...');
}

or declare it as global inside the function to be able to access it

function authenticate($u, $p) {
    global $pdo;
    $que = $pdo->query('...');
}

Upvotes: 0

leftclickben
leftclickben

Reputation: 4614

You need to pass the PDO object as a parameter to the authenticate() function:

function authenticate(PDO $pdo, $u, $p) {
    // ..as in the question..
}

Oh and you should be using a place holder for that username in the query, not string concatenation which is prone to SQL injection attacks.

Upvotes: 1

Related Questions