0

I'm trying for a search function, I made a public function in a file called Topic, and I tried calling the method in the index.php, however it doesn't show anything, I think the problem lies with the while statement, but i'm not entirely sure.

Topic.php

class Topic{

public function searchPosts($postTags)
  {
    $conn = Db::getInstance();

   $statementSearch = $conn->prepare("SELECT * FROM topics INNER JOIN posttopic ON topics.topicID=posttopic.topicID INNER JOIN posts ON posttopic.postID=posts.postID WHERE naam = :naam");
   $statementSearch->bindValue(":naam", $postTags);
   return $statementSearch->execute(array());


  }
}  

Index.php

    spl_autoload_register(function ($class) {
    include_once("classes/" . $class . ".php");
    });  

$_SESSION['KEYWORD'] = array();
$postArray = array();

//$allResults2 = array();

if (isset($_POST['Find'])) {

if (!empty($_POST['Find'])) {

    $searchTopic = new Topic();
    $postTags = $_POST['naam'];
    $searchTopic->searchPosts($postTags);

    while ($row = $statementSearch->fetch(PDO::FETCH_ASSOC)) {
        $_SESSION['KEYWORD'][] = $row['postImageUrl'];
        $postArray[] = $row['postID'];
    }
}


if (count($_SESSION['KEYWORD']) === 0) {
    $error = "Sorry no results!";

}

}

This is the html where it should be printed.

        <?php
        foreach (array_combine($_SESSION['KEYWORD'], $postArray) as           $imageLink => $i) {
            echo "<a href='./pin.php?postid=$i' ><img src='" . $imageLink . "'</a>";

        }
        ?>
  • Why are you passing an empty array to `execute`? – Pyromonk May 10 '17 at 00:00
  • Oops I see it now, I don't know what to put in it though. Is that the problem? – Arne Berckmans May 10 '17 at 08:39
  • What do you mean? Have you copied this code from somewhere? Leaving it empty should be safe: [mysqli_stmt::execute](http://php.net/manual/en/mysqli-stmt.execute.php), but I am making a conjecture, as topic.php is referring to a portion of code you have not presented in your question. I have highlighted this particular issue, because the rest of your code looked fine to me, and this was the only bit that I found problematic. Please consider doing some [debugging](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). – Pyromonk May 10 '17 at 08:53
  • Is that the same for PDO? Also, I have presented everything that involves the search function. The other parts of Topic.php should have no influence on it. – Arne Berckmans May 10 '17 at 10:48
  • My bad, I didn't read the code carefully. Passing a blank array to PDO's [execute](http://php.net/manual/en/pdostatement.execute.php) doesn't make much sense to me either, as you are already binding values to variables right before that statement. Have you tried outputting error messages like I have suggested? This would help tremendously. – Pyromonk May 10 '17 at 11:36
  • Deleted the comment. Its at if(!empty), that he fails. – Arne Berckmans May 10 '17 at 16:48
  • By "fails" do you mean "produces an error" or "returns empty for $_POST['Find']"? Have you checked the [manual entry](http://php.net/manual/en/function.empty.php) for the function? Does your code function if you remove the if block containing `empty`? – Pyromonk May 11 '17 at 01:10

0 Answers0