开发者

>Call to a member function exec() on a non-object< when I try to call PEAR MDB2 class

开发者 https://www.devze.com 2023-04-06 11:36 出处:网络
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:

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 ex开发者_开发问答ec() 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


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().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消