PHP: help with a number of queries

I have a method that goes through all the rows of a table where a certain condition is met, and then checks if one of the columns appears in the row in the text. The method below:

public function isRecipeType($ingredients, $type)
{
    $rows = $this->fetchAll($this->select()->where("type = ?", $type));

    foreach($rows as $row)
    {
        if((strpos($ingredients, $row->name)) !== false)
        {
            return true;
        }
    }
}
However, it lasts forever. How can I speed this up (without deleting rows from the table)?
+3
source share
3 answers

The problem you are trying to solve can be expressed as "are there any lines with type = X whose name contains the text Y?"

, , PHP. , , . , , PHP, , , , . . , SQL , , PHP-.

public function isRecipeType($ingredients, $type)
{
    $sql = "SELECT COUNT(*) AS c ".
           "FROM table ".
           "WHERE type = ? ".
           "   AND ? LIKE CONCAT('%', name, '%')";
    $rows = execute($sql, $type, $ingredients);
    $row = $rows[0];
    return $row["c"] > 0;
}

MySQL, , SQL

           "   AND ? LIKE '%' || name || '%'";

execute , , , , , - .

, , type. : ( ), LIKE.

, , 10% , (.. ) , , . , , , .

+3

-, , type? , , .

+2

SQL?

- - :

"... WHERE `type`=? AND FIND_IN_SET(name, `$ingredients`) > 0 LIMIT 1"

// return TRUE if a row is found

When working, the FIND_IN_SETvalues ​​must be separated by commas, so you may need to convert the variable $ingredients.

+2
source

Source: https://habr.com/ru/post/1769749/


All Articles