0

I've been working on a blog website on localhost. I'm finally ready to move it over to a live server, but many of my queries stopped working! Below is one example of a query that worked, but no longer is working properly. Is there something I'm missing? Thanks in advance :)

public function search($query, $start, $limit)
{
    $stmt = $this->pdo->prepare("SELECT * FROM `articles` WHERE `status` = 1 AND LOWER(`title`) LIKE LOWER(:query) ORDER BY `id` DESC LIMIT :start, :limit");
    $stmt->bindParam(":query", $query, PDO::PARAM_STR);
    $stmt->bindParam(":start", $start, PDO::PARAM_INT);
    $stmt->bindParam(":limit", $limit, PDO::PARAM_INT);
    $stmt->execute();
    $articles = $stmt->fetchAll(PDO::FETCH_OBJ);
    $count = $stmt->rowCount();

    if($count > 0)
    {
        foreach($articles as $article)
        {
            $date = date('F jS, Y', strtotime($article->published));
            echo '<div class="article">
            <h2>'.$article->title.'</h2>
            <h4>'.$article->subtitle.', <span>'.$date.'</span></h4>
            <div class="imageWrapper">
                <img src="'.BASE_URL.$article->banner.'">
                <div class="articleHover">
                    <p>'.$this->shorten(strip_tags($article->content), 300).'...</p>
                </div>              
            </div>
            <div id="mobileDescription"><p>'.$this->shorten(strip_tags($article->content), 300).'...</p></div>
            <a href="article/'.$article->link.'">Read Article</a>
        </div>';
        }
    }
    else
    {
        echo "No Search Results...";
    }

}

No errors are thrown, just seems to return a count of 0 rows (Tested by echoing $count). There is data in the table and everything is set up correctly. If you view the page at https://nerbgamez.ca/blog you can see the articles displaying properly on the home page. It seems to only be an issue with the search. Below I'll show the ajax function used to call this query, maybe this will help.

Ajax function

index = 0;
app = 2;

...

$.ajax({
        url: 'https://nerbgamez.ca/blog/inc/ajaxFunctions.php',
        type: 'POST',
        data: {start:index, limit:app, index:3, query:$('#query').val()},
        success: function (res) {
            $('#searchResults').html(res);                  
        }
    });

ajaxFunctions.php

if($index == 3)
{
    $start = $_POST['start'];
    $limit = $_POST['limit'];
    $query = '%'.$_POST['query'].'%';

    $getFromA->search($query, $start, $limit);
}
Jacob
  • 1
  • 1
  • 1
    Are you connected to a different database on live ? Does this database you are connected to have articles table seeded with data? Just an idea... – Eden Reich Jan 15 '19 at 23:55
  • Be sure whatever code is calling the `search()` method is passing a `$query` value that has wildcards where you want them (like `$query = '%something%'`) or the LIKE will behave like an `=`. But if you haven't changed any of your code, that's probably not the problem. – Don't Panic Jan 15 '19 at 23:58
  • 2
    Because if your count was tested and echoed 0 it means the connection and all the other common mistakes are solved, can you confirm there is data in articles table? – Eden Reich Jan 15 '19 at 23:58
  • Just added some extra info and code snippets. Hopefully it's a little clearer now – Jacob Jan 16 '19 at 02:02
  • 1
    [`PDOStatement::rowCount()`](http://php.net/manual/pdostatement.rowcount.php) is not reliable. Since you've already used `fetchAll()`, you should just use `$count = count($articles);` – Phil Jan 16 '19 at 02:21
  • You say _"no errors are thrown"_ but are you able to see them if they are? Please see [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) including [this answer](https://stackoverflow.com/a/52324601/283366) about PDO – Phil Jan 16 '19 at 02:28
  • A am able to see all errors of they are thrown and I've tried it both with and without it if statement. It returns null either way. – Jacob Jan 16 '19 at 23:51

0 Answers0