GET Variable in "WHERE LIKE ?" - Problem mit Wildcard

loddarmattheus

Erfahrenes Mitglied
Hallo Jungs,
ich habe mal wieder ein kleines Problem in einem Script.
Und zwar übergebe ich per URL die Variable title an das Script: http://localhost/php_rest_myblog/api/post/read1.php?title=LTX_Technology Post Two

Die read1.php greift auf die Klasse post.php zurück und verarbeitet die Daten, unter anderem auch die GET Variable.

read1.php
PHP:
<?php
  // 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);

  // Get ID
  $post->title = isset($_GET['title']) ? $_GET['title'] : die();

  // Blog post query
  $result = $post->read_title();

  // Get row count
  $num = $result->rowCount();

  // Check if any posts
  if($num > 0) {
    // Post array
    //$posts_arr = array();
    $posts_arr['data'] = array(); //das array heisst dann data

    while($row = $result->fetch(PDO::FETCH_ASSOC)) {
      extract($row);
      $post_item = array(
        'id' => $id,
        'title' => $title,
        'body' => html_entity_decode($body),
        'author' => $author,
        'category_id' => $category_id,
        'category_name' => $category_name
      );
      // Push to "data"
      array_push($posts_arr['data'], $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')
    );
  }

Hier wird die Variable title an post.php weitergegeben:
$post->title = isset($_GET['title']) ? $_GET['title'] : die();

post.php
PHP:
<?php
  class Post {
    // DB stuff
    private $conn;
    private $table = 'posts';
    // Post Properties
    public $id;
    public $category_id;
    public $category_name;
    public $title;
    public $body;
    public $author;
    public $created_at;
    // Constructor with DB
    public function __construct($db) {
      $this->conn = $db;
    }
  
    // Get Title Post
    public function read_title() {
      // Create query
      $query = 'SELECT c.name as category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
                                    FROM ' . $this->table . ' p
                                    LEFT JOIN
                                      categories c ON p.category_id = c.id
                                    WHERE
                                    p.title LIKE ?
                                      ORDER BY
                                      p.created_at DESC';
    
      // Prepare statement
      $stmt = $this->conn->prepare($query);
      // Bind ID
      $stmt->bindParam(1, $this->title);
      // Execute query
      $stmt->execute();
      return $stmt;
    }

An der rot markierten Stelle ist das Problem. Ich kann hier nicht .... WHERE p.title LIKE "?%" schreiben. Ich gebe zu, das hier ist nicht mein Script und so richtig kenne ich mich mit prepared Statements nicht aus, aber ich wäre für jeden Tipp dankbar.

Ziel soll es sein, dass ich die GET Variable "title" so an die select-Abfrage weiterreichen kann, um mit dem Wildcardsymbol arbeiten zu können.

Danke vorab. Gruss loddar
 
Zurück