Formatting the results of a MySQL query as if it were run from the console

后端 未结 7 2142
清酒与你
清酒与你 2020-12-16 15:04

I\'m writing a quick and dirty reporting script that queries a report and emails the results. When using the MySQL console the results are in a nicely formatted table:

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 15:30

    I optimized the answer of @ehudokai so it uses less loops (5 vs 9). And for completeness I added the command line, stats and error output, too:

     $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)';
    }
    ?>
    

    Examples

    SELECT

    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)
    

    Error:

    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)
    

提交回复
热议问题