Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php pdo problem

  • 27-07-2015 05:01PM
    #1
    Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭


    I'm getting Fatal error: Call to a member function prepare() on a non-object in /home/a9319665/public_html/davez_gamez/core/class.ManageDatabase.php on line 41 which is this
    $query = $this->link->prepare("SELECT id, picurl1, name, description ,price FROM games");
    I don't understand why because I have function __construct which I thought was supposed to instantiate it.
    Anyway not sure what to do with the error.
    Can anyone help?
    Here's the relevant code.
    class.ManageDatabase.php
    <?php
    include_once 'core/class.Game.php';
        class ManageDatabase
        {
            public $link;
            
            function __construct()
            {
                include_once('class.database.php');
                $conn = new database;
                $this->link = $conn->connect();
                return $this->link;
            }
            
            function getData($table_name, $id=null)
            {
                if(isset($id))
                {
                    $query = $this->link->query("SELECT picurl1,name, description, price FROM $table_name WHERE id = '$id'  ORDER BY id ASC");
                }
                else
                {
                    $query = $this->link->query("SELECT picurl1,name, description, price FROM $table_name  ORDER BY id ASC");
                }
                $rowCount = $query->rowCount();
                if($rowCount >= 1)
                {
                    $query->setFetchMode(PDO::FETCH_ASSOC);
                    $result = $query->fetchAll();
                }
                else
                {
                    $result = 0;
                }
                return $result;
            }
            
            function getGames($table_name, $id=null)
            {
                try {
                    $query = $this->link->prepare("SELECT id, picurl1, name, description ,price FROM games");  //the error line
                     $query->execute();
                    
                    
                } catch ( PDOException $ex ) {
                        echo "An Error occured when retrieving games: " . $ex->getMessage ();
                }
                $rowCount = $query->rowCount();
                if($rowCount<1)
                {
                    $query = 0;
                }
                return $query;
           }
    
    class.database.php
    <?php
        include_once 'config.php';
    
        class database
        {
            protected $db_conn;
            public $db_name = DB_NAME;
            public $db_host = DB_HOST;
            public $db_password = DB_PASSWORD;
            public $db_user = DB_USER;
            
            function connect()
            {
                try
                {
                    $this->db_conn = new PDO("mysql:host =  $this->db_host;dbname=$this->db_name", $this->db_user, $this->db_password);
                    return $this->db_conn;
                }
                catch(PDOException $e)
                {
                    return $e->getMessage();
                }
            }
            
            
        }
    ?>
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 90 ✭✭iioklo


    An observation, on lines 3 and 10 of your first script and line 3 of the second script, the include_once statements you have html tags in your php, remove these and keep the php file name that you want to include.


  • Registered Users, Registered Users 2 Posts: 262 ✭✭Banta


    Have you defined $table_name at any point?

    Can see it called in function getData($table_name, $id=null), but can't see where it's actually defined, so guess I'll start there.


Advertisement