Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize | SQLize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE TABLE stock ( id INT AUTO_INCREMENT, model VARCHAR(100), storage VARCHAR(100), color VARCHAR(100), PRIMARY KEY (id) ); INSERT INTO stock(id, model, storage, color) VALUES (1, 'iPhone 14 Pro', '128gb', 'Black'), (2, 'iPhone 14 Pro', '256gb', 'Red'), (3, 'iPhone 14 Pro', '128gb', 'Black'), (4, 'iPhone 14 Pro', '256gb', 'Black'), (5, 'iPhone 14 Pro', '256gb', 'Red'), (6, 'iPhone 14 Pro', '128gb', 'White'), (7, 'iPhone 13 Pro', '128gb', 'Black'), (8, 'iPhone 14 Pro', '256gb', 'Red');

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php // $mysqli is already declared as a working database connection by this sandbox $result = []; foreach ($mysqli->query("SELECT * FROM stock") as $row) { extract($row); $result[$model][$storage][$color] = ($result[$model][$storage][$color] ?? 0) + 1; } var_export($result);
Copy Clear