简体   繁体   中英

PHP MYSQL PDO Database Connection

I am using PHP to establish a connection to a MYSQL database using PDO. I keep getting this error:

Uncaught Error: Call to a member function query() on null

I am pretty new to this approach but why am I getting a null from the query?

This is the code calling the classes:

<?php

    $data = new Data;
    echo $data->connect();
    
    $view = new View;
    echo $view->getData();
    
?>

This is the query class with the problem I suspect:

<?php 
    
    class View extends Data {
    
        public function getData() {
            $sql = 'SELECT * FROM equipment';
            $stm = $this->connect()->query($sql);
            while ($row = $stm->fetch()) {
                echo $row['manuName'] . '<br>';
            }
        }
    }
    
?>

This is the connection class:

<?php 
class Data {
    private $dbHost = DB_HOST;
    private $dbUser = DB_USER;
    private $dbPass = DB_PASS;
    private $dbName = DB_NAME;

    private $dbHandler;
    private $error;

    public function connect() {
        $con = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
            $this->dbHandler = new PDO($con, $this->dbUser, $this->dbPass, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}

I am not seeing my error. If anyone can see why I cannot pull the data.

Thank you

As u_mulder said in the comments,

your connect method will not return anything.

I updated it as below and made some change on your getData() method:

    public function connect() {
        $con = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
            //$this->dbHandler = new PDO($con, $this->dbUser, $this->dbPass, $options);

            return  new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}


class View extends Data {


    public function getData($table){
        try {
            $sql="SELECT * FROM $table";
            $q = $this->connect()->query($sql) or die("failed!");

            while($r = $q->fetch(PDO::FETCH_ASSOC)){  $data[]=$r;  }
            return $data;

        }
        catch(PDOException $e)
        {
            echo 'Query failed'.$e->getMessage();
        }

    }


}



$view = new View;
$result = $view->getData('equipment');
print_r($result);

Although I prefer remove connect method and add a constructor in your Data class as below:

    public $this->conn;
    public function __construct(){

        $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);

    }

and then change my getData as below:

$q = $this->conn->query($sql) or die("failed!");

‌Because as ADyson said in the comments :

you shouldn't really be connecting again every time you run a query..

Thanks again Ali, ADyson & u_mulder,

If I understand what all comments are saying, this code should factor in your advice. Can you please tell me if this is a better approach if you have the time.

Data Class:

<?php 
class Data {
    private $dbHost = DB_HOST;
    private $dbUser = DB_USER;
    private $dbPass = DB_PASS;
    private $dbName = DB_NAME;
    private $error;

    public $this->conn;
    public function __construct(){

        $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
    }

        try {
            return new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}

View Class:

<?php 

class View extends Data {


    public function getData($table){
        try {
            $sql="SELECT * FROM $table";
            $q = $this->conn->query($sql) or die("failed!");
           
            while($r = $q->fetch(PDO::FETCH_ASSOC)){  $data[]=$r;  }
            return $data;

        }
        catch(PDOException $e)
        {
            echo 'Query failed'.$e->getMessage();
        }

    }


}

Output:

<?php
$view = new View;
$result = $view->getData('equipment');
print_r($result);    
?>

This code is still giving errors:

unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM