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
In dieser post.php mit nur einer Klasse wird die Variable aber benötigt, doch wie bekomme ich die hier "rüber"?
Als Ergebnis liefert mir das Statement nur "
BTCUSDT{"message":"No Posts Found"}"
Ich hoffe, das war irgendwie verständlich beschrieben. DAnke vorab.
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.