如何在不再次查询的情况下从mysql数组中检索前2个结果


How to retrieve first 2 results from mysql array without querying again?

每个页面都列出了特定零售商可用的所有优惠券。我在数据库中查询标题中的所有优惠券代码,因为我计算了返回的行数,并在页面的元标题中使用了这些信息。我现在还想显示数组中前2张优惠券的标题。如何在不再次查询数据库的情况下从数组中提取前2个结果?

这就是我目前所拥有的:

$retailer_coupons = "select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category from tblCoupons C,tblMerchants M where C.merchantid=M.merchantid and C.begin <  ".mktime()." and C.expire > ".mktime()." and C.merchantid=".$merchantid." and M.display='1' and C.user_submitted='' order by C.custom_order desc, C.coupon desc";
$retailer_coupons_result = mysql_query($retailer_coupons) or die(mysql_error());
$count_coupons=mysql_num_rows($retailer_coupons_result);
$meta_title = ''.$name.' Coupon Codes ('.$count_coupons.' coupons available)';

假设我的表中有3条记录。如果我执行下面的查询,我会得到2的结果,但count(*)会给我3作为输出

SELECT count(*) FROM temp.maxID limit 2

在你的情况下,它将是

$retailer_coupons =
    "select C.couponid,C.fmtc_couponid,C.merchantid,C.exclusive,C.label,C.shoppingtip,C.restrictions,C.coupon,C.custom_order,C.link,C.image,C.expire,C.unknown,M.name,M.approved,M.homepageurl,M.category
    from tblCoupons C,tblMerchants M
    where C.merchantid=M.merchantid
    and C.begin <  ".mktime()." and C.expire > ".mktime()."
    and C.merchantid=".$merchantid." and M.display='1'
    and C.user_submitted=''
    order by C.custom_order desc, C.coupon desc
    limit 2";

limit 2会变魔术。。。干杯

祝你好运!!!

类似这样的东西:

$res = mysql_fetch_assoc($retailer_coupons_result);
$i = 0;
while ($i < 2){
    echo $res[$i]['label']."'n";
    $i++;
}