0

I'm beginning with PHP and i need your help.

I create a try to list all members who has the same interest that the current_member( i mean the connected member ).

I write this :

$current_members = params('current_member');
    $members_all = option('db')->query('SELECT * FROM members WHERE interest = $current_members["interest"] ORDER BY lastname, firstname')->fetchAll();

    set('members_all', $members_all);

When I go on my page I have the error : Fatal error: Call to a member function fetchAll() on a non-object

And in my view I just write this :

        <h2 id="member-<?= $member['id'] ?>">

            <?= avatar_tag($member,'30x30') ?>

            <a href="<?=member_url_for($member)?>"><?=$member['firstname']?><small> <?= $member['lastname'] ?></small></a>

        </h2>

I dont understand this error, anyone can help me ?

Thank's for your help.

  • 2
    The call to `query` probably failed. You can see that by storing the result of the call in a variable. – user703016 Jun 19 '14 at 20:16
  • 1
    Your SQL query did not complete successfully. Do *not* ever just *assume* your query succeeded. You need to check for errors *before* calling `fetchAll()`. Chaining `query()->fetchAll()` is bad practice. Do something like `$query = query(); if($query){ $query->fetchAll(); } else{ die(mysqli_error($query); }` (or whatever, that's just pseudo-code). – gen_Eric Jun 19 '14 at 20:16
  • hum , ok how i can do that ? – Florian Dano Clement Jun 19 '14 at 20:17

3 Answers3

2

Do not chain calls to query() and fetchAll() like you are doing. That's bad practice. Never assume your query worked, always check to see if it did.

$db = option('db');
$query = $db->query('SELECT ...');
if($query === FALSE){
    print_r($db->errorInfo());
    die;
}
$members_all = $query->fetchAll();

(Since you are calling fetchAll(), I assume you are using PDO (and not MySQLi))

Also, do not try to concatenate variables into your SQL query. (P.S. You're not even doing that, you are using single quotes, so $current_members["interest"] is not being read as a variable) That's just asking for an SQL injection attack. What you want to do is use prepared statements.

$db = option('db');
$query = $db->prepare('SELECT * FROM members WHERE interest = ? ORDER BY lastname, firstname');
$exec = $query->execute(array($current_members['interest']));
if($exec === FALSE){
    print_r($query->errorInfo());
    die;
}
$members_all = $query->fetchAll();
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

IN your sql query

'SELECT * FROM members WHERE interest = $current_members["interest"] ORDER BY lastname, firstname'

you are using single quotes, so $current_members["interest"] actually does not resolve to a PHP variable, it is a string. You can switch single and double quotes:

Made an edit here, passing array offset was not fortunate:

$interest = $current_members['interest'];
"SELECT * FROM members WHERE interest = $interest ORDER BY lastname, firstname"

Unfortunately you did not share any of the underlying database code, but assuming option('db') is a pdo object, this should work fine.

If option('db') really is a pdo, before executing any statement add:

option('db') -> setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );

this will tell you the exact error

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Sesertin
  • 462
  • 2
  • 11
  • if i try this : $members_all = option('db')->query('SELECT * FROM members ORDER BY lastname, firstname')->fetchAll(); I can list all members. But if i add WHERE, that fail. – Florian Dano Clement Jun 19 '14 at 21:13
0

The "call to a member function on a non-object" error, means that you are trying to call a method on a variable that does not represent an object.

You have the following methods called one after the other on the $members_all denifition: option('db')->query("...")->fetchAll();

You call the method "query" of whatever returns option('db') with some SQL query, and then you call fetchAll() method to whatever returns that "query" method.

I do not know if I explained myself well, the main point is that when you execute the query method it is returning something that has not the "fetchAll" method, in your case your SQL is wrong and probably query() is returning NULL or FALSE instead of a result set.

Change your single quotes with double quotes or concatenate the $current_member['interest'] variable.

aaronfc
  • 190
  • 8