0

I'm trying to do a dynamic query building with this answer but apparently, I'm missing something out:

When I do $db->select($fields,$where, $params);, it returns undefined variable on db.

Please, don't mind the unparameterized query as I can't find a way to fix that (maybe I'll do another question).

Here goes my code so far:

db_connection:


<?php

$servername = ""; // credentials removed
$username = "";
$password = "";
$database =  "";

try {
    $conn = new PDO("sqlsrv:server=$servername;Database=$database", $username, $password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   //echo "Connected successfully\n";
    }
catch(PDOException $e)
    {
    //echo "Connection failed: " . $e->getMessage();
    }
?>

readRecords.php


include 'db_connection.php';
        // Recolher dados POST

        class db {

            public $conn;

            public function select($fields = '', $where = '1', $params = array(), $limit = '') { //fetchArgs, etc
                //$fields = implode(', ', $this->dbFields);

                //create query
                $query = "SELECT $fields FROM $databaseInUse WHERE $where $limit";

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

                return $stmt->execute($params);

                //$stmt->fetchAll($fetchStyle);
            }
            //...
        }


        date_default_timezone_set("Europe/Lisbon");

        $secondDate = date("Y-m-d",strtotime("+1 day", strtotime((String)$dataInit)));

        try
        {
            $fields = "MAX(DATEPART(hh,Datetime)) AS hora, COUNT(ID) AS prod";
            $where = "DateTime > '".$dataInit." 00:00' 
            AND DateTime < '".$dataInit." 16:30' 
            AND GlobalResult=1

            GROUP BY DATEPART(hh,Datetime);";
            $params = "";
            $db->select($fields,$where, $params);

/*          $sql= $conn->prepare("SELECT MAX(DATEPART(hh,Datetime)) AS hora, COUNT(ID) AS prod

            FROM $databaseInUse

            WHERE DateTime > '".$dataInit." 00:00' 
            AND DateTime < '".$dataInit." 16:30' 
            AND GlobalResult=1

            GROUP BY DATEPART(hh,Datetime);");

            //$sql->bindValue(':database', $databaseInUse);
            $sql->execute(); */



        }

        catch (PDOException $e){echo $e->getMessage();

CaldeiraG
  • 152
  • 2
  • 14

1 Answers1

0

You use the $db variable in readRecords.php, but it has not been defined anywhere. Neither in this file nor in any of your other files you've posted.

There are many strange things in your code anyway, so it does not surprise me that it does not work (a miracle that it even works).

The $conn property in your class db is not initialisied anywhere. Your select method will attempt to access the $conn property dynamic (under your comment "// prepare statement"). Did you mean $this->conn->query... instead?

Daniel S
  • 71
  • 7
  • As mentioned, I followed the linked question method, isn't `$db->select($fields,$where, $params);` supposed to call the class function? – CaldeiraG Aug 01 '19 at 08:11
  • 1
    @CaldeiraG It does, and then you are accessing and undefined property `$conn` which actually is the error and the cause. You have never initialised `$conn`. – Code Spirit Aug 01 '19 at 09:09
  • @CodeSpirit thank you! I managed to fix it, based on a question in the duplicate chain of questions and realized that. – CaldeiraG Aug 01 '19 at 09:11