开发者

Sorting a list from MySQL alphabetically using two tables for input data

I have the following PHP:

<?php
$con = mysql_connect("localhost","foo","bar");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("zed", $con);
$getTagID = $_GET["id"];
//make a loop that outputs each coupon that has been tagged that

$alphaTagSQL = mysql_query("SELECT * FROM coupon_tags WHERE tagID = '$getTagID'");

$alphaTagSQLArr[] = mysql_fetch_array($alphaTagSQL);

foreach($alphaTagSQLArr as $mouse)
    {
        $nzed = $mouse["couponID"];
        $brah = mysql_query("SELECT * FROM coupons WHERE couponID = '$nzed'");
        $pen[] = mysql_fetch_row($brah);
    }

foreach($pen 开发者_运维技巧as $ruler)
    {
        echo $pen;
    }

?>

What I'm trying to do is to select the tagID from the $_GET, then match it up to all of the couponID's that match it in the table coupon_tags, then match those coupon_ID's to retailerName in the table coupons, where couponID would match up to the field id. Then I just want to list them alphabetically. I'm just running into a few problems - namely, I get the error Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource.

Any help?


Try to change

$brah = mysql_query("SELECT * FROM coupons WHERE couponID = '$nzed'"); 

into

$brah = mysql_query("SELECT * FROM coupons WHERE couponID = '$nzed'") or die(mysql_error());

It will give you more information on why the query didn't succeed.


If you're using a recent version of MySQL, I'd suggest replacing the loop with a subselect, e.g.

"SELECT retailerName FROM coupons WHERE couponID IN (SELECT id FROM coupon_tags WHERE tagID = '$getTagID')"

If you put more into your queries, you can let the database do the work, and it will be more efficient than multiple queries. It's also usually a good idea to only return the columns you're interested in rather than all of the columns.

Apart from that, you could try to see what the issue is using the MySQL console or more verbose PHP logging (like what @Ikke suggests)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜