致命错误:在布尔值中调用成员函数prepare() [英] Fatal error: Call to a member function prepare() on boolean in

查看:75
本文介绍了致命错误:在布尔值中调用成员函数prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我冲了几个问题,但没人帮忙.
致命错误:调用成员函数布尔值上的bind_param()输入->不.
致命错误:调用成员函数prepare() null ->不.
致命错误:在布尔值->不.
致命错误在null上调用成员函数prepare() ->不.
对资源上的成员函数prepare()的致命错误调用->不.
错误:调用成员函数prepare()在非物体上->不.我完成了.

I red several questioins, but no one helped.
Fatal error: Call to a member function bind_param() on boolean in -> nope.
Fatal error: Call to a member function prepare() on null -> nope.
Fatal error: Call to a member function count() on boolean -> nope.
Fatal error Call to a member function prepare() on null -> nope.
fatal error call to a member function prepare() on resource -> nope.
Error: Call to a member function prepare() on a non-object -> nope. I am done..

我在PDO中使用PHP5和mySql:

I am using PHP5 and mySql with PDO:

连接和选择"工作正常,但插入"不想工作.
那是我的功能:

Connection and Select works fine, but the Insert didnt want to work.
That's my function:

function AddNewUser($nickname, $email)
{
    ini_set('display_errors', 1); //DELETE ME
    ini_set('expose_php', 1); //DELETE ME

    $pdo = EstablishDBCon();
    echo "Subscribe user..<br/>";

    $sql = "INSERT INTO db.table (nickname, email, insertdate, updatedate) VALUES (:nickname, :email, :insertdate, :updatedate)";

    try {
        $stmt = $pdo->prepare($sql); //Error at this line
        //id?
        $stmt->bindParam(':nickname', $nickname, PDO::PARAM_STR);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
        $stmt->bindParam(':insertdate', date("Y-m-d H:i:s"), PDO::PARAM_STR);
        $stmt->bindParam(':updatedate', null, PDO::PARAM_NULL);
        $stmt->exeute();

        CloseDBCon($pdo);
        echo "Subscribed!<br/>";
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}

数据库模式为:
id(int不能为null auto_inc)|昵称(varchar不为null)|电子邮件(varchar不为null)| insertdate(日期时间)|更新时间(日期时间)

The DB pattern is:
id (int not null auto_inc) | nickname (varchar not null) | email (varchar not null) | insertdate (datetime) | updatedate (datetime)

我是php的新手,我不理解这种类型的错误. 我在代码中标记了抛出错误的行:

I am new to php and I do not understand that type of error. I marked the line inside the code, where the error is thrown:

$stmt = $pdo->prepare($sql); //Error at this line

有人可以帮助我吗?

提前谢谢!

// 连接又名db_connection.php:

// Connection aka db_connection.php:

<?php
echo 'Establishing MySQL Connection<br/>';

$pdo = null;
$dsn = 'mysql: host=xx; dbname=xx';
$dbUser = 'xx';
$pw = 'xx';

try {
    $pdo = new PDO($dsn, $dbUser, $pw);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo 'Connection established.<br/>';
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

return $pdo;
?>

这是BuildDBCon函数:

Here is the EstablishDBCon function:

function EstablishDBCon()
{
    $pdo = include_once 'db_connection.php';
    return $pdo;
}

推荐答案

重用函数的最佳方法是将其放在include文件中,然后将其包含在需要的每个文件的顶部.因此,在您的db_connection.php内部,创建函数:

The best way to reuse functions is to put it inside of the include file, then include it at the top of each file you'll need it. So inside of your db_connection.php, create your function:

function EstablishDBCon()
{
    $pdo = false;
    try{ 
         // Put your PDO creation here
    } catch (Exception $e) {
        // Logging here is a good idea
    }
    return $pdo;

}

现在,您可以在需要的地方使用该功能.在使用$pdo !== false之前,请确保始终确保它没有出现故障.

Now you can use that function wherever you need it. Make sure you always make sure $pdo !== false before you use it, to make sure your connection hasn't failed.

这篇关于致命错误:在布尔值中调用成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆