Aerdan
Aerdan

Reputation: 51

Convert Array for mySQL Query

I have two queries. The first one is generating a list of ids (col_id_8) I want to use in the second query. I'm not sure what I am doing wrong. Thank you!

$sql = 'SELECT DISTINCT col_id_8 FROM exp_channel_grid_field_84 WHERE entry_id = :entry_id';
    $stmt = $conn->prepare($sql);

    try {
        $stmt->execute(array('entry_id' => $entry_id));
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result as $row) {
            $fullcodes[] = $row['col_id_8'];
        }
    } catch(PDOException $e) { error_log($e->getMessage(),0); }

    $sql = 'SELECT DISTINCT name FROM stix_live_orders WHERE code in (:allcodes) ORDER BY name ASC LIMIT 4';
    $stmt = $conn->prepare($sql);

    try {
        $stmt->execute(array('allcodes' => $fullcodes));
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        # MORE CODE
    }

Upvotes: 0

Views: 46

Answers (1)

trzyeM-
trzyeM-

Reputation: 943

Try $stmt->execute(array('allcodes' => implode( ',', $fullcodes ) )); instead $stmt->execute(array('allcodes' => $fullcodes));

Upvotes: 2

Related Questions