1

I have the following function:

public function detail($detail, $table, $column, $value) {
        if(is_array($detail)) {
            $data = array();

            foreach($detail as $key) {
                $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?");
                if(is_numeric($value)) {
                    $stmt->bind_param('i', $value);
                } else {
                    $stmt->bind_param('s', $value);
                }
                $stmt->execute();
                $stmt->bind_result($detail);
                $stmt->fetch();
                $data[] = $detail;
            }

            return $data;
        } else {
            $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?");

            if(is_numeric($value)) {
               $stmt->bind_param('i', $value);
            } else {
                $stmt->bind_param('s', $value);
            }
            $stmt->execute();
            $stmt->bind_result($detail);
            $stmt->fetch();

            return $detail;
        }
    }

This functions works well untill I use an array. The way to use this function is something like this: $db->detail('username', 'users', 'id', 1) This would return the username from users where the id is 1 (this works fine). Like I said the problem starts when I use an array so for example:

$details = array('username', 'active', 'registered');
$details = $db->detail($details, 'users', 'id', 1);
print_r($details);

The error is pointing to $stmt->bind_param('i', $value); in the if(is_array()). I already tried the answer of: bind_param on a non-object but that didn't help me; I still get the same error. I hope someone knows how to fix the Fatal error: Call to a member function bind_param() on a non-object error for me.

Thanks in advance.

2
  • most likely because prepare() failed, thats why when you binded, it didn't work Commented Sep 21, 2014 at 10:06
  • might be because you are overwriting variable $detail which fails the prepare in next iteration Commented Sep 21, 2014 at 10:26

2 Answers 2

1

Try to unset the $stmt variable in the loop:

public function detail($detail, $table, $column, $value) {
    if(is_array($detail)) {
        $data = array();

        foreach($detail as $key) {
            $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?");
            if(is_numeric($value)) {
                $stmt->bind_param('i', $value);
            } else {
                $stmt->bind_param('s', $value);
            }
            $stmt->execute();
            $stmt->bind_result($detail);
            $stmt->fetch();
            $data[] = $detail;
            $stmt = null;
        }

        return $data;
    } else {
        $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?");

        if(is_numeric($value)) {
            $stmt->bind_param('i', $value);
        } else {
            $stmt->bind_param('s', $value);
        }
        $stmt->execute();
        $stmt->bind_result($detail);
        $stmt->fetch();

        return $detail;
    }
}

This should help.

Sign up to request clarification or add additional context in comments.

2 Comments

That is strange because it is working normal on my server. Did you added the line $stmt = null; at the end of foreach body?
I noticed I made a typo somewhere in my array thats why I still fot the same error but now it works. ty!
1

I think the efficient way to prepare query without using loop is too implode values in the array instead of looping and preparing the query statement. For eg:- if the query is

SELECT `username`,`active`,`register` FROM users WHERE ID = 1 //username,active and register can be used in single prepare statement by imploding the array


if(is_array($detail)) {
    $data = array();

        $stmt = $this->mysqli->prepare("SELECT ".implode(", ",$detail)." FROM `$table` WHERE `$column` = ?");
        if(is_numeric($value)) {
            $stmt->bind_param('i', $value);
        } else {
            $stmt->bind_param('s', $value);
        }
        $stmt->execute();
        $stmt->bind_result($detail);
        $stmt->fetch();
        $data[] = $detail;
        $stmt = null;

    return $data;
}

1 Comment

This will not work because of 2 reasons: 1. you have to remove ` ` ` from the query otherwise the query will not be correct; 2. in bind_result statement you have to pass each column separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.