简体   繁体   中英

Select products of a certain category and brand

I have a quert where i want to select all products that have the category and brand selected on url:

http://localhost/www.fermaster.pt/listaProdutos/category1/brand-1

"SELECT prod.*
 FROM produtos as prod
 INNER JOIN categorias as cat
    ON cat.nomeCategoria LIKE '".$categoria."'
 INNER JOIN marcas as m
    ON m.nomeCategoria LIKE '".$marca."'
 WHERE prod.categoriaProduto = cat.ID
    AND prod.marcaProduto = m.ID"

It gives me the error

Warning: Illegal string offset 'nomeProduto' in C:\\wamp\\www\\www.fermaster.pt\\pages\\listaProdutos.php on line 32

The full code is:

<?php
$db = new Database();    
$db->connect();


if(isset($marca))
{
    echo 'Produto - Categoria - Marca';
    $db->sql("SELECT prod.*
              FROM produtos as prod
              INNER JOIN categorias as cat
                ON cat.nomeCategoria LIKE '".$categoria."'
              INNER JOIN marcas as m
                ON m.nomeCategoria LIKE '".$marca."'
              WHERE prod.categoriaProduto = cat.ID
                    AND prod.marcaProduto = m.ID");
}
else
{
    echo 'Produto - Categoria';
    $db->sql("SELECT prod.*
              FROM produtos as prod
              INNER JOIN categorias as cat
                ON cat.nomeCategoria LIKE '".$categoria."'
              WHERE prod.categoriaProduto = cat.ID");
}

$res = $db->getResult();

foreach($res as $output)
{
    echo '<br />'.$output['nomeProduto'];
}

?>

I think you have to escape the string first because of the / elements. MySQLs QUOTE() function is your friend here. Check the MySQL Manual on that topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM