Jack
Jack

Reputation: 11

Array incorrectly returning empty

I'm a bit new at php so still learning the in's and out's. The specifics of what I'm actually doing are mostly irrelevant.

The code below is meant to connect to a database, perform a query and then return an array filled with the corresponding rows. At the moment the array has data when it is printed within the searcher function and is empty when I examine it later.

function searcher ($connector, $select, $from, $where)
{
    $profile = array();

    $query = $connector->prepare('SELECT ' . $select . ' FROM ' . $from . ' WHERE ' . $where);
    $query->execute();

    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        $profle[] = $row;
    }

    print_r($profle);
    return $profile;
}

$connector = db_connection ('localhost', 'sakila', 'root', 'admin');

$search[] = searcher($connector, '*', 'actor', 'actor_id = 1 OR actor_id = 2');
echo "<br><br>";
print_r($search); 

The output is as follows:

Array ( [0] => Array ( [actor_id] => 1 [first_name] => PENELOPE [last_name] => GUINESS [last_update] => 2006-02-15 04:34:33 ) [1] => Array ( [actor_id] => 2 [first_name] => NICK [last_name] => WAHLBERG [last_update] => 2006-02-15 04:34:33 ) )

Array ( [0] => Array ( ) )

Any idea what I'm doing wrong? I'm fairly sure its something simple as nothing that complicated is going on.

Upvotes: 0

Views: 90

Answers (2)

silkfire
silkfire

Reputation: 25935

Your $profile variable is misspelled. Probably that's why.

The code is perfectly legit though, which is the reason that you're not getting any warnings or notices from the parser.

Upvotes: 2

dmnptr
dmnptr

Reputation: 4304

In fact, you have 2 different arrays in your searcher function - $profile and $profle. In the function you fill $profle with values and output it. That's why you get output in the first case. However, your function returns empty array $profile, which you output in main program. That's why you get empty array in the second case.

Upvotes: 0

Related Questions