Jump to content

Pdo group by


Recommended Posts

Im trying to build a trading script ive coded the listing part just fine and the offering part i am now trying to display how many offers there are on each item.

 

The offer on is what they are offering on so i have a table with all the items in then in the offer table i store the id of the offered on item

 

    $stmt = $db->prepare("SELECT count(*) FROM trade_offers WHERE offer_on = ?");
$stmt->execute([$result->id]);
$count = $stmt->fetchColumn();

so i would like to group by offer_on so atm my code would show 2 results but there is only 1 offer with 2 items. So the person has offered 2 items in exchange for item with id 1

Screenshot_7.png

Link to comment
Share on other sites

    $query2 = mysqli_query($link,"SELECT * FROM `trade_offers` WHERE `offer_on`='{$result->id}' GROUP BY `offer_on`");
            $numOffers = mysqli_num_rows($query2);

This does what i need it todo buts its mysql and id like to use pdo
    

Link to comment
Share on other sites

6 minutes ago, geatzo said:
$result->id

this implies you are doing this based on the result of some other query. don't do that. use a single appropriate type of JOIN query to get the data that you want all at once.

at the sql query statement level, the syntax is the same for both the mysqli and PDO extensions. i recommend that you build any sql query statement in a php variable, so that the sql query syntax is separated as much as possible from the php syntax -

$sql = "SELECT count(*) FROM trade_offers WHERE offer_on = ? GROUP BY offer_on";

 

Link to comment
Share on other sites

Posted (edited)
12 minutes ago, mac_gyver said:

this implies you are doing this based on the result of some other query. don't do that. use a single appropriate type of JOIN query to get the data that you want all at once.

at the sql query statement level, the syntax is the same for both the mysqli and PDO extensions. i recommend that you build any sql query statement in a php variable, so that the sql query syntax is separated as much as possible from the php syntax -

$sql = "SELECT count(*) FROM trade_offers WHERE offer_on = ? GROUP BY offer_on";

 

so like this 

        $stmt = $db->prepare("SELECT count(*) FROM trade_offers WHERE offer_on = ? GROUP BY offer_on");
$stmt->execute([$result->id]);
$count = $stmt->fetchColumn();

which shows 2 results still instead of the 1  i am doing a while loop to display all the users items i then display how many offers are on each item inside the while loop.

Edited by geatzo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.