Frage

I am confused with the include stuff i think, I don't know where exactly its wrong.

The Connection file with fluentpdo

    <?php
error_reporting(E_ALL | E_STRICT);
include($_SERVER['DOCUMENT_ROOT'].'/Mark20/libs/FluentPDO/FluentPDO.php');

$pdo = new PDO("mysql:dbname=ummah", "pluto","admin");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$fpdo = new FluentPDO($pdo);
//~ $software->debug = true;
?>

if i am using to insert data like this as below: Insert.php

<?php
include '../connect.inc.php';

function inReg(){
    try{
        $values = array('name' 'xyz', 'pwd' => '1234');
        $query = $fpdo->insertInto('users')->values($values)->execute();
        echo 'success';
        return;
    }catch (Exception $e) {
        die ('File did not upload: ' . $e->getMessage());
    }
}
?>

Not a problem with above stuff, its just in a php file. What I am trying to do is use a class as i did below: Connection file is same.

DBInsert.php

<?php

include($_SERVER['DOCUMENT_ROOT'].'/Mark20/bin/connect.inc.php');

class DBInsert {
    function Insert($table, $values){
        try{
            $query = $fpdo->insertInto($table)->values($values)->execute();
            return 'success';
        }catch (Exception $e) {
            die ('File did not upload: ' . $e->getMessage());
        }
    }
}

?>

now i am creating object of above class and trying to call the Insert function like this: Test.php

include($_SERVER['DOCUMENT_ROOT'].'/Mark20/bin/dao/DBInsert.php');

function Signup(){
    $values = array('name' => 'xyz', 'pwd' => '1234');
    $db = new DBInsert();
    echo $db->Insert('users',$values);
}
Signup();
?>

So when i am accessing Test.php i am getting the following error:

Notice: Undefined variable: fpdo in E:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Mark20\bin\dao\DBInsert.php on line 15

Fatal error: Call to a member function insertInto() on a non-object in E:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Mark20\bin\dao\DBInsert.php on line 15

I am new to php, not totally new also :). So a little help is greatly appreciated.

Thanks and Regards Adeeb

War es hilfreich?

Lösung

Global variables are not in scope in functions by default. You need to do:

function inReg(){
    global $fpdo;
    try{
        $values = array('name' 'xyz', 'pwd' => '1234');
        $query = $fpdo->insertInto('users')->values($values)->execute();
        echo 'success';
        return;
    }catch (Exception $e) {
        die ('File did not upload: ' . $e->getMessage());
    }
}

or pass $fpdo as a parameter to the function.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top