H
H
hellion352021-12-07 10:52:17
PHP
hellion35, 2021-12-07 10:52:17

Why is it not looking for news in the database by title?

Good afternoon. I take news from the regional site:

$html = file_get_contents('https://my-domain.ru');
$doc = phpQuery::newDocument($html);
$news = array();
$entry = pq("article")->find(".lenta-big-item");

foreach($entry as $row ){
    $row = pq($row);
    $title = $row->find('.lenta-title')->text();
    $i = search_news(trim($title));
    
    if( $i ){
        $news[] = $row->find('.lenta-title')->attr('href');
    }
}

The search_news() function should search for news by title, and if no news is found, return true.
Here is the function itself:
function search_news( $title = '' ){
    include "../includes/config.php";
    $query = "SELECT * FROM news WHERE news_title LIKE '%$title%'";
    $r = mysqli_query($connect, $query);

    if($r->num_rows == 0) {
        return true;
    }else{
        return false;
    }
    mysqli_close($connect);
}

The problem is that if I pass the title in the $title variable, then the news is not found in the database. If I redefine the $title variable in the function ($title = 'News title'), then the news is successfully searched in the database. I don't understand what could be wrong? Encoding? I don’t understand what needs to be done so that the news is normally searched for by the variable passed to the function. Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2021-12-07
@hellion35

https://habr.com/en/post/563530/ As galaxy
correctly wrote in the comments, if the database "does not find" something, you need to convert both strings to a binary representation using bin2hex() and see the difference. and then add str_replace based on the results

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question