ho ottimizzato the answer of @ehudokai in modo che utilizza meno cicli (5 vs 9). E per completezza ho aggiunto la linea di comando, le statistiche e output di errore, anche:
<pre>
<?php
$db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$start = microtime(true);
$sql = "SELECT * FROM myTable";
$result = mysqli_query($db, $sql);
$exec_time = microtime(true) - $start;
// obtain the maximum string length of all column headings and rows
$colwidths = array();
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $key => $value) {
// heading
if (!isset($colwidths[ $key ])) {
$colwidths[ $key ] = strlen($key) + 2;
}
// rows
$colwidths[ $key ] = max($colwidths[ $key ], strlen($value) + 2);
}
}
echo 'mysql>' . trim($sql) . PHP_EOL;
// SELECT, SHOW, DESCRIBE, EXPLAIN = resource
// INSERT, UPDATE, DELETE, DROP = true
// Error = false
if (!is_bool($result)) {
if ($colwidths) {
mysqli_data_seek($result, 0);
while ($row = mysqli_fetch_assoc($result)) {
// create and display horizontal line and column headings
if (!isset($header)) {
$header = '| ';
$line = '+';
foreach ($row as $key => $value) {
$line .= str_repeat('-', $colwidths[ $key ] + 2) . '+';
$header .= str_pad($key, $colwidths[ $key ]) . ' | ';
}
echo $line . PHP_EOL;
echo $header . PHP_EOL;
echo $line . PHP_EOL;
}
// display row values
foreach ($row as $key => $value) {
echo '| ' . str_pad($value, $colwidths[ $key ] + 1);
}
echo '|' . PHP_EOL;
}
echo $line . PHP_EOL;
}
mysqli_free_result($result);
}
$affectedrows = mysqli_affected_rows($db);
if ($result === false) {
echo PHP_EOL . 'ERROR ' . mysqli_errno($db) . ': ' . mysqli_error($db);
}
else if ($result === true) {
echo 'Query OK, ' . $affectedrows . ' rows affected (' . round($exec_time/$iterations * 1000) . ' ms)';
}
else if ($affectedrows) {
echo $affectedrows . ' rows in set (' . round($exec_time/$iterations * 1000) . ' ms)';
}
else {
echo 'Empty set (' . round($exec_time/$iterations * 1000) . ' ms)';
}
?>
</pre>
Esempi
SELEZIONA
mysql>SELECT
topic_id,
MATCH(text) AGAINST('tuning') AS score
FROM
topics
WHERE
MATCH(text) AGAINST('tuning' IN BOOLEAN MODE)
ORDER BY
score DESC
LIMIT 10
+----------+--------------------+
| topic_id | score |
+----------+--------------------+
| 153257 | 5.161948204040527 |
| 17925 | 4.781417369842529 |
| 66459 | 4.648380279541016 |
| 373176 | 4.570812702178955 |
| 117173 | 4.55166482925415 |
| 167016 | 4.462575912475586 |
| 183286 | 4.4519267082214355 |
| 366132 | 4.348565101623535 |
| 95502 | 4.293642520904541 |
| 29615 | 4.178250789642334 |
+----------+--------------------+
10 rows in set (141 ms)
Errore:
mysql>SELECT * WHERE 1=1
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1=1' at line 1
UPDATE
mysql>UPDATE topics_search SET topic_id = topic_id WHERE topic_id = 2
Query OK, 0 rows affected (0 ms)
No, è necessario formattarla te stesso. Quando ottieni risultati per mysql che non usi il client mysql, stai usando php e le librerie mysql. La formattazione è qualcosa fatto dal client della riga di comando. Se vuoi la stessa formattazione, dovrai farlo tu stesso. Se vuoi aiutare a farlo, non dovrebbe essere troppo difficile. – ehudokai
@ehudokai Capisco le differenze tra le librerie mysql di PHP e la console. Speravo davvero in qualcosa di più intelligente. –
Capisco :) Ho scritto cosa avrebbe dovuto essere fatto nella mia risposta, ma se non ti dispiace i moduli PEAR Console_Table che @mfonda ha detto sembra che faccia la stessa cosa. – ehudokai