PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE my_table ( id INT UNSIGNED AUTO_INCREMENT, type INT NOT NULL, PRIMARY KEY (id) ); CREATE INDEX my_idx ON my_table (type); CREATE VIEW my_view AS (SELECT type from my_table UNION ALL SELECT type from my_table); INSERT INTO my_table (type) VALUES (1), (1), (1), (2), (2), (3);
Copy Clear
Copy Format Clear
<?php $subQuery = 'SELECT type from my_table UNION ALL SELECT type from my_table'; $query = "EXPLAIN EXTENDED SELECT * FROM ($subQuery) as q WHERE type = ?;"; //$query = "EXPLAIN EXTENDED SELECT * FROM my_view WHERE type = ?;"; $type = 2; $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $stmt = $pdo->prepare($query); $stmt->bindParam(1, $type, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); echo createTable($result); $stmt = $pdo->query('SHOW WARNINGS', PDO::FETCH_ASSOC); $result = $stmt->fetchAll(); echo createTable($result); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $stmt = $pdo->prepare($query); $stmt->bindParam(1, $type, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); echo createTable($result); $stmt = $pdo->query('SHOW WARNINGS', PDO::FETCH_ASSOC); $result = $stmt->fetchAll(); echo createTable($result); function createTable($array) { $horizontal = '─'; $vertical = '│'; $cross = 'o'; $crosses = [ 'nw' => '┌', 'n' => '┬', 'ne' => '┐', 'e' => '┤', 'se' => '┘', 's' => '┴', 'sw' => '└', 'w' => '├', 'c' => '┼', ]; // Get the headers (keys of the first element) $headers = array_keys($array[0]); // Calculate column widths $columnWidths = []; foreach ($headers as $header) { $columnWidths[$header] = strlen($header); } foreach ($array as $row) { foreach ($row as $key => $value) { $columnWidths[$key] = max($columnWidths[$key], strlen((string) $value)); } } // Build the table $firstRow = $crosses['nw']; $headerRow = $vertical; $deviderRow = $crosses['w']; foreach ($headers as $header) { $firstRow .= str_repeat($horizontal, $columnWidths[$header] + 2) . $crosses['n']; $headerRow .= ' ' . str_pad($header, $columnWidths[$header]) . ' ' . $vertical; $deviderRow .= str_repeat($horizontal, $columnWidths[$header] + 2) . $crosses['c']; } $firstRow = rtrim($firstRow, $crosses['n']) . $crosses['ne']; $deviderRow = rtrim($firstRow, $crosses['c']) . $crosses['e']; $table = $firstRow . "\n" . $headerRow . "\n" . $deviderRow . "\n"; foreach ($array as $row) { $rowLine = $vertical; foreach ($headers as $header) { $rowLine .= ' ' . str_pad((string) $row[$header], $columnWidths[$header]) . ' ' . $vertical; } $table .= $rowLine . "\n"; } $table .= $divider . PHP_EOL . PHP_EOL; return $table; }
Show:  
Copy Clear