Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

php pdo problem

  • 27-07-2015 4: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