SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fatal error: Call to a member function prepare() on a non-object

    I keep getting

    Code:
    Fatal error: Call to a member function prepare() on a non-object in /var/www/auto/functions.php on line 72
    for the following but i have checked all post variables all are being passed.

    Can any one spot my error?!


    PHP Code:
    function add_device ($devicename,$devicebrand,$deviceremoteid,$devicechannel) {
    GLOBAL 
    $dbh;
    $insertdev$dbh->prepare("INSERT INTO `devices` (`name`, `brand`, `remoteid`, `channel`) VALUES (:name, :brand, :remote, :channel)");
    $insertdev->bindParam(':name'$devicename);
    $insertdev->bindParam(':brand'$devicebrand);
    $insertdev->bindParam(':remote'$deviceremoteid);
    $insertdev->bindParam(':channel'$devicechannel);
    $insertdev->execute();
    echo 
    "<br><b>Device Added to Database</b><br />";


  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,053
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Without my getting into a discussion on using "global", does it "work" if you declare the global outside of the function?

  3. #3
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope
    Code:
    Notice: Undefined variable: dbh in /var/www/auto/functions.php on line 72
    
    Fatal error: Call to a member function prepare() on a non-object in /var/www/auto/functions.php on line 72

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,053
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Where is $dbh coming from? Maybe the problem is there?

  5. #5
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $dbh is declared in an include config.php file.

    funny thing is the other functions work spot on just how i've done the above it appears to be only this one function that is throwing a wobbler

    this is my functions script

    https://raw.github.com/txt3rob/RPI-C.../functions.php

  6. #6
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very weird i have resolved it by doing an include inside the function of the config file although it's already declared at the top and global has been set!

  7. #7
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    If you resolved it by including a config file the global variable is apparently defined inside the config file. Just by declaring it as global will not work if the global variable does not even exist in the very first place, thats why you have to include/import it from a config file or whatever file it was initially defined. Do not confuse global variables with superglobals, the latter is available everywhere in your script but a global variable has to be defined first.

    On the other hand, I recommend against using global variable in the very first place. Its an extremely bad programming practice no matter whether you use procedural, functional or OO programming paradigm, a habit you need to get rid of before its too late. Instead, you can use dependency injection by passing the $dbh object to the very function/method you are calling.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •