SELECT `product_no`, `commit_date`, `non_green_desc` FROM `non_green` AS ng INNER JOIN `non_green_reasons` AS ngr ON ng.`ng_id` = ngr.`ng_id` WHERE `complete_date` = ? GROUP BY `tank_no`
ASKER
ASKER
<?php // demo/mysqli_example.php
/**
* https://www.experts-exchange.com/questions/29012010/show-child-records-separated-by-commas.html
*/
error_reporting(E_ALL);
echo '<pre>';
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
$err
= "CONNECT FAIL: "
. $mysqli->connect_errno
. ' '
. $mysqli->connect_error
;
trigger_error($err, E_USER_ERROR);
}
// CREATING A TABLE FOR OUR TEST DATA
$sql
=
"
CREATE TEMPORARY TABLE item_defects
( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, name VARCHAR(24) NOT NULL DEFAULT ''
, d_key INT NOT NULL DEFAULT 0
)
"
;
$mysqli->query($sql);
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('car', 5)");
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('car', 3)");
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('car', 7)");
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('bike', 9)");
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('bike', 2)");
$mysqli->query("INSERT INTO item_defects (name, d_key) VALUES ('bike', 5)");
// CREATING A TABLE FOR OUR TEST DATA
$sql
=
"
CREATE TEMPORARY TABLE defects
( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, d_key INT NOT NULL DEFAULT 0
, defect VARCHAR(24) NOT NULL DEFAULT ''
)
"
;
$mysqli->query($sql);
$mysqli->query("INSERT INTO defects (d_key, defect) VALUES (5, 'Scratch')");
$mysqli->query("INSERT INTO defects (d_key, defect) VALUES (3, 'Broken Window')");
$mysqli->query("INSERT INTO defects (d_key, defect) VALUES (7, 'Burst Tyre')");
$mysqli->query("INSERT INTO defects (d_key, defect) VALUES (9, 'Broken Light')");
$mysqli->query("INSERT INTO defects (d_key, defect) VALUES (2, 'Missing Seat')");
$sql = "SELECT name, defect FROM item_defects, defects WHERE item_defects.d_key = defects.d_key ORDER BY name DESC";
$res = $mysqli->query($sql);
$out = [];
$old = FALSE;
while ($row = $res->fetch_object())
{
if ($row->name != $old)
{
$old = $row->name;
$out[$row->name] = ucfirst($row->name) . ': ' . $row->defect;
}
else
{
$out[$row->name] .= ', ' . $row->defect;
}
}
var_dump($out);
ASKER
$old = $row->name;
echo "<tr>";
echo "<td>" . $out[$row->name] = ucfirst($row->name) . "</td>";
echo "<td>" . $row->defect . ", " . "</td>";
echo "</tr>";
ASKER
ASKER
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
Also, you can put your questions into more than one Topic Area. The MySQL topic area seems appropriate for this.