Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can I not select the description of a product in my database?

Hello this my first time on Stack Overflow and i am a begginer in php and phpMyAdmin. I have an error that i can’t understand.
I have a search bar with a redirect button. I can succesfully search a product by writing "cere" (cereal in my database).
Clicking the button will give me every details i have for the product cereal.

This code below is my code at the moment.

function SearchProducts(PDO $db, string $research): array
{
    $datas = array(
        'research' => '%' . $research . '%'
    );
    
    $sql = "SELECT * FROM product WHERE name LIKE :research WHERE description LIKE :research";
    $qry = $db->prepare($sql);
    
    $qry->execute($datas);
    $products = $qry->fetchAll(); 
    
    return $products;
}

My problem is, i want to be able to search the product "cereal" with its description also that i have in my database.
It gives me an error saying that my "WHERE description LIKE" is wrong. Can someone help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Its kind of difficult to know if you have other errors, since you didn’t post your html, but the error your getting is only because you have a seconde WHERE. If you want to be able to search a product with its description, you can use OR instead of the second WHERE.
Hope it helps.

$sql = "SELECT * FROM product WHERE name LIKE :research OR description LIKE :research";
$qry = $db->prepare($sql);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading