Ok so Im using the following query
SELECT
`inquiry_source`.`source` AS 'Source',
`inquiry_source`.`colour` AS 'Colour',
SUM(
CASE WHEN `inquiry_source`.`source` = `customer_migration_details`.`Acquisition_Source` THEN 1 ELSE 0
END
) AS 'count'
FROM `inquiry_source`
JOIN `customer_migration_details`
ON `customer_migration_details`.`Acquisition_Source` = `inquiry_source`.`source`
WHERE `date_opened` = '2023-01-23' AND `Acquisition_Source` != ''
GROUP BY `Acquisition_Source`
Which in phpMyAdmin show the following results
| Source | Colour | count |
|---|---|---|
| Auto-Trader | #cc0099 | 1 |
| Ebay/tel | #ffbb33 | 1 |
| eShot | #00b3b3 | 1 |
| #0044cc | 11 | |
| #ff6666 | 3 | |
| Web | #668cff | 11 |
However when I come to the php as follows.
$sql= "SELECT
`inquiry_source`.`source` AS 'Source',
`inquiry_source`.`colour` AS 'Colour',
SUM(
CASE WHEN `inquiry_source`.`source` = `customer_migration_details`.`Acquisition_Source` THEN 1 ELSE 0
END
) AS 'count'
FROM `inquiry_source`
JOIN `customer_migration_details`
ON `customer_migration_details`.`Acquisition_Source` = `inquiry_source`.`source`
WHERE `date_opened` = '2023-01-23' AND `Acquisition_Source` != ''
GROUP BY `Acquisition_Source`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
foreach($row as $k => $v){
if($k == 'Source'){
$seriesData[] = "'{$v}'";
}
}
}
return implode(',', $seriesData);
Its only returning the following (Missing out Auto-Trader)
Ebay/tel,
eShot,
FaceBook,
Google,
Web
Is there somthing I am missing?
I’ve tried changing the query, add various htmlentites(), htmlspecialchars(), addslashes() etc. but to no avail.
>Solution :
This line
while($row = $result->fetch_assoc()) {
returns one row from the resultset
Then this line returns the next
foreach($row as $k => $v){
So you read and ignored the first row of your resultset