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

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

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

Copy Format Clear
CREATE TABLE aircrafts ( id INT AUTO_INCREMENT PRIMARY KEY, aircraft_type VARCHAR(100) NOT NULL, operation_years TINYINT UNSIGNED NOT NULL, in_maintenance BOOLEAN DEFAULT FALSE, hours_flown DECIMAL(10,2) NOT NULL, registration_date DATETIME DEFAULT CURRENT_TIMESTAMP, maintenance_notes TEXT ); CREATE TABLE maintenance_records ( id INT AUTO_INCREMENT PRIMARY KEY, aircraft_id INT, maintenance_cost DECIMAL(10,2) NOT NULL, maintenance_date DATETIME DEFAULT CURRENT_TIMESTAMP );

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

Copy Clear
Copy Format Clear
<?php $pdo->beginTransaction(); $insertQuery = "INSERT INTO aircrafts (aircraft_type, operation_years, in_maintenance, hours_flown, registration_date) VALUES "; $rows = []; for ($i = 1; $i <= 100000; $i++) { $type = "Model " . rand(1, 50); $years = rand(1, 30); $maintenance = rand(0, 1); $hours = round(rand(100, 10000) + rand(0, 99) / 100, 2); $date = date("Y-m-d H:i:s", strtotime("-" . rand(0, 3650) . " days")); $rows[] = "('$type', $years, $maintenance, $hours, '$date')"; if ($i % 1000 == 0) { $pdo->exec($insertQuery . implode(", ", $rows)); $rows = []; } } if (!empty($rows)) { $pdo->exec($insertQuery . implode(", ", $rows)); } $pdo->commit(); $sql = " SELECT a.id, a.aircraft_type, a.operation_years, a.hours_flown, a.registration_date, COALESCE(AVG(m.maintenance_cost), 0) AS avg_maintenance_cost FROM aircrafts a LEFT OUTER JOIN maintenance_records m ON a.id = m.aircraft_id GROUP BY a.id LIMIT 10"; $stmt = $pdo->query($sql); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <table border="1"> <thead> <tr> <th>ID</th> <th>Aircraft Type</th> <th>Years in Operation</th> <th>Hours Flown</th> <th>Registration Date</th> <th>Avg Maintenance Cost</th> </tr> </thead> <tbody id="data-table"></tbody> </table> <script> $(document).ready(function() { const data = <?php echo json_encode($data); ?>; $.each(data, function(i, row) { $("#data-table").append(` <tr> <td>${row.id}</td> <td>${row.aircraft_type}</td> <td>${row.operation_years}</td> <td>${row.hours_flown}</td> <td>${row.registration_date}</td> <td>${row.avg_maintenance_cost}</td> </tr> `); }); }); </script> </body> </html>
Copy Clear