PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE table1 ( id INT UNSIGNED AUTO_INCREMENT, type INT NOT NULL, PRIMARY KEY (id) ); CREATE INDEX my_idx ON table1 (type); INSERT INTO table1 (type) VALUES (1), (1), (1), (2), (2), (3);
Copy Clear
Copy Format Clear
<?php use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use JetBrains\PhpStorm\ExpectedValues; use PDO; use PDOException; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableStyle; use Symfony\Component\Console\Output\BufferedOutput; $view = 'SELECT type from table1 UNION ALL SELECT type from table1'; $query = "EXPLAIN SELECT * FROM ($view) as v WHERE type = ?;"; $type = 2; $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); dump($result); function dumpTable( array|Collection $data, string $style = 'box' ): mixed { if (is_array($data)) { $data = collect($data); } $array = $data->map(fn ($row) => (array) $row)->all(); $output = new BufferedOutput(); $table = new Table($output); $table->setStyle($style); $table->setHeaders(array_keys($array[0] ?? [])); $table->setRows($array); $table->render(); $tableCode = htmlspecialchars(trim($output->fetch())); if ($style === 'markdown') { $tableRows = explode("\n", $tableCode); array_shift($tableRows); array_pop($tableRows); $tableCode = implode("\n", $tableRows); } echo "<pre style=\"margin-bottom: 40px\">$tableCode</pre>"; return $data; }
Show:  
Copy Clear