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
Share      Blog   Popular

PHPize.online is a free online environment for quickly running, experimenting with and sharing PHP (including Carbon extension for DateTime) and SQL code. You can run your SQL code with PHP code that can use the same DB. For database manipulations you can use pre-defined instances of PDO ($pdo), mysqli ($mysqli) & Laravel query builder ($db)

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; $middleRow = $crosses['w']; $lastRow = $crosses['sw']; foreach ($headers as $header) { $firstRow .= str_repeat($horizontal, $columnWidths[$header] + 2) . $crosses['n']; $headerRow .= ' ' . str_pad($header, $columnWidths[$header]) . ' ' . $vertical; $middleRow .= str_repeat($horizontal, $columnWidths[$header] + 2) . $crosses['c']; $lastRow .= str_repeat($horizontal, $columnWidths[$header] + 2) . $crosses['s']; } $firstRow = mb_substr($firstRow, 0, -1) . $crosses['ne']; $middleRow = mb_substr($middleRow, 0, -1) . $crosses['e']; $lastRow = mb_substr($lastRow, 0, -1) . $crosses['se']; $table = $firstRow . "\n" . $headerRow . "\n" . $middleRow . "\n"; foreach ($array as $row) { $rowLine = $vertical; foreach ($headers as $header) { $rowLine .= ' ' . str_pad((string) $row[$header], $columnWidths[$header]) . ' ' . $vertical; } $table .= $rowLine . "\n"; } $table .= $lastRow . PHP_EOL . PHP_EOL; return $table; }
Show:  
Copy Clear