>调用非对象上的成员函数 exec()<当我尝试调用 PEAR MDB2 类时

发布于 2024-12-05 15:40:20 字数 1885 浏览 1 评论 0 原文

我有一个问题,我似乎无法自己解决,尽管脚本有点简单......我只是想写一些东西。在 MySQL 数据库(auto_increment id)中使用以下脚本:

<?php
// Create a valid MDB2 object named $mdb2
// at the beginning of your program...
require_once 'MDB2.php';

// Once you have a valid MDB2 object named $mdb2...

class addToDb extends MDB2  {

    function __construct() {

        $mdb2 =& MDB2::connect('mysql://************************');
            if (PEAR::isError($mdb2)) {
                die($mdb2->getMessage());
            }

    }

    // 1) Add general information into trips
    function addTrip()  {

        $title = $_POST['title'];
        $author = $_POST['author'];
        $description = $_POST['description'];
        $date_start = $_POST['date_start'];
        $date_end = $_POST['date_end'];

        if(isset($title)) echo $title;
        else echo "!!";

        //$id = $mdb2->extended->getAfterID($id);

        $sql  = "INSERT INTO trips (title, author, description, date_start, date_end) 
                VALUES ($title, $author, $description, $date_start, $date_end)";

        $affected =& $mdb2->exec($sql);

        // Always check that result is not an error
        if (PEAR::isError($affected)) {
            die($affected->getMessage());
        }

    }


    // Disconnect
    function disconnectDb() {

        $mdb2->disconnect();

    }

}


?> 

这就是我想要调用对象的方式:

    $input = new addToDb();
    $input->addTrip();
    $input->disconnectDb();

我尝试了很多事情,包括只执行代码而不将其放入类中,总是出现相同的错误:

Fatal error: Call to a member function exec() on a non-object in /www/htdocs/w007bba1/v3/_class/_general/_db.php on line 36

第 36 行代表

$affected =& $mdb2->exec($sql);

我的addToDb 类。如果有人能告诉我我的脚本哪里不正确,我将不胜感激,到目前为止我在其他帖子中找不到任何帮助......

问候! 斯托基

I have a problem which I seem unable to solve on my own, although the script is kind of simple... I simply want to write sth. in a MySQL database (auto_increment id) with the following script:

<?php
// Create a valid MDB2 object named $mdb2
// at the beginning of your program...
require_once 'MDB2.php';

// Once you have a valid MDB2 object named $mdb2...

class addToDb extends MDB2  {

    function __construct() {

        $mdb2 =& MDB2::connect('mysql://************************');
            if (PEAR::isError($mdb2)) {
                die($mdb2->getMessage());
            }

    }

    // 1) Add general information into trips
    function addTrip()  {

        $title = $_POST['title'];
        $author = $_POST['author'];
        $description = $_POST['description'];
        $date_start = $_POST['date_start'];
        $date_end = $_POST['date_end'];

        if(isset($title)) echo $title;
        else echo "!!";

        //$id = $mdb2->extended->getAfterID($id);

        $sql  = "INSERT INTO trips (title, author, description, date_start, date_end) 
                VALUES ($title, $author, $description, $date_start, $date_end)";

        $affected =& $mdb2->exec($sql);

        // Always check that result is not an error
        if (PEAR::isError($affected)) {
            die($affected->getMessage());
        }

    }


    // Disconnect
    function disconnectDb() {

        $mdb2->disconnect();

    }

}


?> 

And that's how I want to call the object:

    $input = new addToDb();
    $input->addTrip();
    $input->disconnectDb();

I have tried many things including just executing the code without putting it in a class, always the same error:

Fatal error: Call to a member function exec() on a non-object in /www/htdocs/w007bba1/v3/_class/_general/_db.php on line 36

Line 36 represents

$affected =& $mdb2->exec($sql);

in my addToDb class. I'd be thankful if somebody could tell me where my script is incorrect, I couldn't find any help in other posts so far...

Regards!
Stocki

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2024-12-12 15:40:20

正如错误所说,似乎 $mdb2 不是对象。

您的问题是您初始化了 $mdb2,但该变量仅在 __construct 范围内可用。您必须将其存储在类变量中才能在 addTrip() 中使用它。

Seems as if $mdb2 is no object, AS THE ERROR SAYS.

Your problem is that you initialize $mdb2, but the variable is only available in the scope of __construct. You have to store it in a class variable to be able to use it in addTrip().

棒棒糖 2024-12-12 15:40:20

PDO 使用 Cpanel 用户和密码,而不是 DB 用户和密码。

PDO uses Cpanel user and password instead of DB user and password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文