0

我有 2 个不同的表:categoriesproducts。每个表都有category_id列。为了在 时输出数组SELECT-ing,我使用的是echo htmlentities.

category_id=categories在产品category_id中选择产品名称和价格等字段的 SQL 语法是什么?(行标题相同 -> 名称(在产品上)和名称(在 cat 上)。

请记住,我首先要根据产品表中的标准选择产品,然后再category_id从 cat 中选择产品。表等于产品表中的 category_id 然后它将显示类别名称(在类别表中)

这是原始代码:

<?php
include('includes/global.php');

$sql = $db->query("SELECT name FROM ".DB_PREFIX."products WHERE active=1 AND closed=0 AND deleted=0 AND approved = 1 AND name LIKE '%".htmlentities($_GET["search"],  ENT_QUOTES)."%' LIMIT 20");

while($product_array = $db->fetch_array($sql)) {
    echo htmlentities($product_array["name"], ENT_QUOTES, $lang[codepage])."\n";
}

这是我尝试过的:

$sql = $db->query("SELECT a.name,a.buy_price,c.name FROM ".DB_PREFIX."products a WHERE active=1 AND closed=0 AND deleted=0 AND approved = 1 AND name LIKE '% ".htmlentities($_GET["search"],  ENT_QUOTES)."%' INNER JOIN ".DB_PREFIX."categories c ON a.category_id = c.category_id LIMIT 20");

while($product_array = $db->fetch_array($sql)) {
    echo htmlentities($product_array["name"], ENT_QUOTES, $lang[codepage])." for ";
    echo htmlentities($product_array["buy_price"], ENT_QUOTES, $lang[codepage])." from category ";
    echo htmlentities($product_array[" * categoryname"], ENT_QUOTES, $lang[codepage])."\n";
}

有什么建议么?

4

2 回答 2

0

我认为这是一个排序问题,整个表需要先构建。

SELECT a.name,a.buy_price,c.name FROM ".DB_PREFIX."products a 
INNER JOIN ".DB_PREFIX."categories c ON a.category_id = c.category_id 
WHERE active=1 AND closed=0 AND deleted=0 AND approved = 1 AND name LIKE '% ".htmlentities($_GET["search"],  ENT_QUOTES)."%' 
LIMIT 20
于 2013-04-12T17:19:13.120 回答
0

这是我得到的最终代码,在搜索时我没有得到任何输出(在相应的搜索框中)。我个人认为合并两个表中的数据存在问题。或者也许我这不是回显 htmlentities 的正确方法。

$sql = $db->query("SELECT a.name,c.name,a.buyout_price FROM ".DB_PREFIX."auctions a INNER JOIN ".DB_PREFIX."categories c ON a.category_id = c.category_id WHERE active=1 AND closed=0 AND deleted=0 AND approved = 1 AND a.name LIKE '% ".htmlentities($_GET["search"],  ENT_QUOTES)."%' LIMIT 20");

while($product_array = $db->fetch_array($sql)) {
    echo htmlentities($product_array["a.name"], ENT_QUOTES, $lang[codepage])." from category ";
    echo htmlentities($product_array["c.name"], ENT_QUOTES, $lang[codepage])." for ";
    echo htmlentities($product_array["a.buyout_price"], ENT_QUOTES, $lang[codepage])." EUR \n";}
于 2013-04-15T08:12:39.527 回答