MySQLi致命错误

时间:2014-04-12 20:03:20

标签: php mysqli

我有 index.php 页面,在标题标签中我有这样的内容:

<title><?php echo getBasic('title'); ?></title>

它返回以下错误:

  

致命错误:在 12 <上的C:\ Program Files \ WAMP \ www \ Filmovi \ modules \ database \ dbcon.php中的非对象上调用成员函数bind_param() < / p>


使用require_once('modules / database / dbcon.php')在索引顶部包含 in dbcon.php 我有这个:

function getBasic($type){
    global $db;
    $sql='SELECT content FROM a853_filmovi WHERE type = ?';
    $stmt = $db->prepare($sql);
    $stmt->bind_param('s',$type);    <-- Line 12
    $stmt->execute();
    $stmt->bind_result($content);
    return $content;
}

在第3行,我有这个:

$public = getBasic('public');

它完美无缺。


顺便说一句,这个工作并正确地显示了标题,然后由于一个未知的原因停止了工作。我不知道如何使用getBasic('public')而不是标题。我在数据库中有一个类型为'title'的记录,所以这不是问题。

提前致谢。

2 个答案:

答案 0 :(得分:2)

这样的错误发生是因为在使用它们之前没有检查返回值。

在这种情况下,错误发生是因为$db->prepare($sql)失败,返回false,然后您将其用作语句(stmt)对象。

在使用之前检查您的返回值:

$stmt = $db->prepare($sql);
if ($stmt === false) {
    die('Preparing SQL string failed');
}

答案 1 :(得分:0)

错误的一个原因是,prepare()失败了 -

if the sql statement sent to it is not valid in the current DB. 
prepare() will then return false.

Eg - if the table name is not correct or one or more field in the query does not exist.