2014-09-21 69 views
1

我有以下功能:bind_param()在非对象错误

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; 
     } 
    } 

此功能运作良好,直到我使用的阵列。使用这个函数的方式是这样的:$db->detail('username', 'users', 'id', 1)这将返回id为1的用户的用户名(这可以很好地工作)。就像我说的,当我使用数组因此,例如问题开始:

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

错误指向$stmt->bind_param('i', $value);if(is_array())。我已经试过了:bind_param on a non-object的回答,但这并没有帮助我;我仍然得到同样的错误。 我希望有人知道如何修复我的Fatal error: Call to a member function bind_param() on a non-object错误。

在此先感谢。

+0

很可能是因为'准备()'失败,这就是为什么当你绑定,它没有工作 – Ghost 2014-09-21 10:06:34

+0

可能是因为您覆盖变量$详细其中失败准备在下一次迭代 – 2014-09-21 10:26:52

回答

1

尝试取消设置$stmt变量在循环:

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; 
    } 
} 

这应该帮助。

+0

这仍然给我同样的错误 – SuperDJ 2014-09-21 10:23:42

+0

这很奇怪,因为它在我的服务器上正常工作。你在'foreach'主体的末尾添加了'$ stmt = null;'这行吗? – Zeusarm 2014-09-21 10:26:10

+0

是的,我确实把它放在foreach循环的末尾 – SuperDJ 2014-09-21 10:27:24

1

我认为在不使用循环的情况下准备查询的有效方法就是在数组中使用implode值,而不是循环和准备查询语句。 对于例如: - 如果查询是

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; 
} 
+0

这将不起作用,因为2个原因:1.必须从查询中删除'''否则查询将不正确; 2.在'bind_result'语句中,你必须分别传递每列。 – Zeusarm 2014-09-21 10:41:53

相关问题