致命错误说明"在非对象上调用成员函数bind_param()"

时间:2015-02-21 08:59:32

标签: php database sqlbindparameter

我遇到了一个致命的错误,我不知道错在哪里,有人可以帮忙吗?

我的分类页面,

<?php 
require_once 'dbfunction.php'; 
require_once 'DBCategory.php';

$con=getDbConnect();
$categoryArr=getcategoryArrCon($con, STATUS_ACTIVE); 

foreach ($categoryArr as $categoryName=>$CategoryInfoArr) { 

?>

这是我的getCategoryArrCon函数脚本

<?php

define("STATUS_ACTIVE", 0);
define("STATUS_DELETE", 1);

function getcategoryArrCon($con, $status) {
    $result = array();
    if (!$con->connect_error) {
        $sqlstr = "SELECT c.category, c.title, c.image" .
                "FROM category c WHERE status=?";
        $stmt = $con->prepare($sqlstr);
        $stmt->bind_param("i", $status);
        $stmt->excute();
        $stmt->bind_result($category, $title, $image);
        while ($stmt->fetch()) {
            $result[$category] = [
                        "category" => $category,
                        "title" => $title,
                        "image" => $image
            ];
        }
        $stmt ->close();
    }
    return $result;
}
?>

1 个答案:

答案 0 :(得分:1)

如果您注意到,当您回复准备好的陈述时,它将如下所示:

SELECT c.category, c.title, c.imageFROM category c WHERE status=?

这个简单的空间在c.imageFROM的中间也很重要,可以使查询有效。

$sqlstr = "SELECT c.category, c.title, c.image" .
                " FROM category c WHERE status=?";
             //  ^

或者为什么不把它变成一个班轮:

$sqlstr = "SELECT c.category, c.title, c.image FROM category c WHERE status = ?";

并确保$stmt->excute();不是真正的拼写错误。其->execute()

旁注:另外,使用->error检查准备工作是否正常。例如:

$stmt = $con->prepare($sqlstr) or die($con->error);