duanhui5344 2013-02-24 19:48
浏览 5

将PDO对象发送到类[关闭]

I'm new here... I have a problem using PDO object in my class... Here is the code: Creating PDO connection:

<?php 
    try {
        $konekcija = new PDO(DSN, USERNAME, PASSWORD);
        $konekcija->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo "Error connecting to database: " . $e->getMessage();
        die();
    }
?>

Class:

class Nastavnik
{
    private $zvanje = "";
    private $konekcija;

    public function __construct($konekcija){
        $this->konekcija = $konekcija;
    }

    public function registracija($korIme, $lozinka, $ime, $prezime, $telefon, $email, $zvanje, $tipKorisnika){
        $upitRegistracija = "INSERT INTO projekat_vst.korisnici (kor_ime, lozinka, ime, prezime, telefon, email, zvanje, tip_korisnika) 
                                                            VALUES (:korIme, :lozinka, :ime, :prezime, :telefon, :email, :zvanje, :tip_korisnika);";
        try {
            $stmt = $konekcija->prepare($upitRegistracija);
            $stmt->execute($korIme, $lozinka, $ime, $prezime, $telefon, $email, $zvanje, $tipKorisnika);
            return 1;
        } catch (PDOException $e) {
            echo "Greška pri kreiranju korisnika." . $e->getMessage();
            return 0;
        }
    }


}

And this is code creating new class:

$korisnik = new Nastavnik($konekcija);

When I try to create new instance of class Nastavnik I get this message:

Notice: Undefined variable: konekcija in D:\php_dev\xampp\htdocs\projekatVST\includes\klasaNastavnik.php on line 24

I don't see where am I making mistake... Thank you for helping! :)

  • 写回答

2条回答 默认 最新

  • dongpi9494 2013-02-24 19:50
    关注

    It's not $konekcija but $this->konekcija.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分