GET_Variable in eine separate Klasse übergeben

loddarmattheus

Erfahrenes Mitglied
Hi Jungs, mal wieder ein kleines Problem, wo ich mich zu blond anstelle.

Ich habe eine URI z.B. ***.de/read.php?coin=BTCUSDT
In der read.php wird die Variable coin eingelesen, muss aber in die includete Post.php weitergegeben werden, da ich damit eine SQL auslesen will.
Hier die read.php
Code:
?php

  $coin = $_GET["coin"];
  echo $coin;

  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json'); 
  include_once '../config/Database.php';
  include_once '../models/Post.php';
 
 
  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();
  // Instantiate blog post object
  $post = new Post($db);
  // Blog post query
  $result = $post->read();
  // Get row count
  $num = $result->rowCount();
  // Check if any posts
  if($num > 0) {
    // Post array
    $posts_arr = array();
    // $posts_arr['data'] = array();
    while($row = $result->fetch(PDO::FETCH_ASSOC)) {
      extract($row);
      $post_item = array(
        'time' => $ticker_ts,
        'symbol' => $symbol,
        'bidprice' => $bidPrice,
        'bidqty' => $bidQty,
        'askprice' => $askPrice,
        'askqty' => $askQty
      );
      // Push to "data"
      array_push($posts_arr, $post_item);
      // array_push($posts_arr['data'], $post_item);
    }
    // Turn to JSON & output
    echo json_encode($posts_arr);
  } else {
    // No Posts
    echo json_encode(
      array('message' => 'No Posts Found')
    );
  }

In dieser post.php mit nur einer Klasse wird die Variable aber benötigt, doch wie bekomme ich die hier "rüber"?
Code:
<?php

  class Post {
    // DB stuff
    private $conn;
    private $table = 'binance_ticker';
    private $coin;
    // Post Properties
    public $ticker_id;
    public $ticker_ts;
    public $symbol;
    public $bidPrice;
    public $bidQty;
    public $askPrice;
    public $askQty;   
    // Constructor with DB
    public function __construct($db) {
      $this->conn = $db;
    }
    // Get Posts
    public function read() {
      // Create query
      $query = 'SELECT ticker_ts, symbol, bidPrice, bidQty, askPrice, askQty
                                FROM ' . $this->table . '
                                WHERE symbol = "' . $this->coin . '"
                                ORDER BY
                                ticker_ts DESC';
      
      // Prepare statement
      $stmt = $this->conn->prepare($query);
      // Execute query
      $stmt->execute();
      return $stmt;
    }   
  }

Als Ergebnis liefert mir das Statement nur "
BTCUSDT{"message":"No Posts Found"}"

Ich hoffe, das war irgendwie verständlich beschrieben. DAnke vorab.
 
coin ist im post.php privat.
Es gibt viele Möglichkeiten. zB über einen Setter
PHP:
public  function  setCoin($coin){
    $this->coin = $coin;
}
Dann musst du im read.php den coin übergeben
PHP:
$post = new Post($db);
$post->setCoin($coin);

ACHTUNG
Noch viel wichtiger! Du übernimmst $coin ungeprüft aus dem $_GET und gisbt sie als SQL-String direkt weiter. Das öffnet gigantische Sicherheitslücken. Da kann man locker un der URL ganze Befehle mitgeebn die deine DB killen/umchreiben etc.
Was du dagegen machen kannst - dafür gibt es hundere Tutorials.
 

Neue Beiträge

Zurück