-1

I've written some PHP code using PDO. I'm new at PHP syntax, so I'm following the PHP documentation. I'm getting a syntax error on the $params = array(... statement. Perhaps the syntax issue is the reference to the global variables, but that's the reference I found in the PHP documentation.

Here's my code:

<?php

/*
    Sets up a PDO object for the ResDatLog table
*/

class Resdatalog
{  
    // database connection and table name
    private $_conn;
    private $_table_name = "ResDataLog";

    // object properties
    public $ID;
    public $Location = null;
    public $Temperature;
    public $Humidity;
    public $Pressure;
    public $RecDate;
    public $AmbientTemp;
    public $AmbientHum;
    public $AmbientPressure;

    /* 
        constructor with $db as database connection
    */
    public function __construct($db)
    {
        $this->conn = $db;
    }

    /* 
        Reads all columns of the entire table ($table_name)
    */
    function read() 
    {
        // select all query
        $query = "SELECT *
                FROM
                " . $this->table_name . "
                ORDER BY 
                    id DESC";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // execute query
        $stmt->execute();
        return $stmt;
    }
    function insert()
    {
        // insert a new row into the table_name table
        $sql = 'INSERT Location, Temperature, Humidity, Pressure, RecDate,
         AmbientTemp, AmbientHum, AmbientPressure into '
         . $this->table_name . 
         ' VALUES (:Location,:Temperature, :Humidity, 
         :Pressure, :RecDate, :AmbientTemp, :AmbientHum, :AmbientPressure)';

        $sth = $this->conn->prepare($sql);

        $params = array(
            ':Location' => ResDataLog->Location,
            ':Temperature'=> ResDataLog->Temperature,
            ':Humidity'=> ResDataLog->Humidity,
            ':Pressure'=> ResDataLog->Pressure,
            ':RecDate'=> ResDataLog->RecDate,
            ':AmbientTemp'=> ResDataLog->AmbientTemp,
            ':AmbientHum'=> ResDataLog->AmbientHum,
            ':AmbientPressure'=> ResDataLog->AmbientPressure);


         Return($$sth->execute($params));
    }
}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
JimS-CLT
  • 665
  • 4
  • 13
  • 30
  • where does ResDataLog come from? Show that code. – Grumpy Apr 23 '20 at 16:12
  • the function insert() is inside the ResDatLog class. So are the variables shown ($Location, et al). ResDataLog->Location refers to $Location, public in the same class, but outside the scope of insert(). – JimS-CLT Apr 23 '20 at 16:13
  • It should be `Resdatalog::Location`, same for all the elements. Notice also the capitalization "ResDataLog" is not correct. – Aioros Apr 23 '20 at 16:14
  • 1
    It should not be `Resdatalog::Location` because it's not a static member! – Alon Eitan Apr 23 '20 at 16:18
  • OK, I fixed the syntax you mentioned (thank you so much...) but I continue to get (from VSCode) syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')' – JimS-CLT Apr 23 '20 at 16:19
  • Replace Resdatalog-> with [$this->](https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php) (don't use the double colons because the class properties aren't static). Also, you have an extraneous $ in `Return($$sth->execute($params));` – Peter Apr 23 '20 at 16:20
  • When I use $this->, do I use the dollar sign in front of the public variable? (in other words...$this->$Location, or $this->Location) – JimS-CLT Apr 23 '20 at 16:24

1 Answers1

2

You can do it with $this as you are inside the class instead of using direct class name.

  1. If you were outside of the class then you need to make an instance of Resdatalog and then access/assign values to the variables like below.

     $ResdatalogInstance = new Resdatalog;
     $ResdatalogInstance->Location="assign some values";
    
  2. If it were static like public static $Location;, then you can do this way, Resdatalog::$Location or self::$Location.

    $params = array(
      ':Location' => $this->Location,
       ':Temperature'=> $this->Temperature,
      ':Humidity'=> $this->Humidity,
      ':Pressure'=> $this->Pressure,
      ':RecDate'=> $this->RecDate,
      ':AmbientTemp'=> $this->AmbientTemp,
      ':AmbientHum'=> $this->AmbientHum,
      ':AmbientPressure'=> $this->AmbientPressure
    

    );

Also you need to remove extra $ sign from this line,

 return ($sth->execute($params));
        ^^
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • 1
    The next question by the OP will probably be about this line `Return($$sth->execute($params));`... – Alon Eitan Apr 23 '20 at 16:26
  • I used Always Sunny's code exactly, and it has no syntax error. I suspect this may be because I've put each parameter on separate lines in my code. Thank you Sunny! – JimS-CLT Apr 23 '20 at 16:29
  • @JimS-CLT glad it helps you somehow, sorry I couldn't format the code properly from my phone :( – A l w a y s S u n n y Apr 23 '20 at 18:06