views:

178

answers:

4

Mysql errors are among the most common ones you see during development, and I am searching for a pleasant and informative way to output them.

phpMyAdmin apparently has a decent pretty-printer for mySQL queries:

parsed query

However, it prints errors as simple text:

error output

Whereas in the latter example, the text after for the right syntax to use near could have been highlighted in the query. Like this:

alt text

Even though it's a short query, you immediately see there's a surplus comma right before the text. Doing so in much larger queries could increase readability dramatically, I'm surprised numerous searches on the subject came up with nothing.

Question in one sentence: Do you use or know of any such PHP class that displays pretty mysql errors for debugging purposes?

+6  A: 

This is really quite trivial for you to do it with a custom function:

function printPrettyError($sql, $error) {
    $display = preg_replace("~^(.*to use near ')(.*)(' at line [0-9]+)$~s", '$1<u>$2</u>$3', $error);
    $display .= "<br />$sql";
}

You will have to tailor it up and decide if you want to make more formatting items to the $sql statement (IE add line breaks before FROM and ORDER BY etc.) But should give you a start.

Brad F Jacobs
I know I can make it myself, but the question is whether there is a (well) written class for this already.
Raveren
Nothing comes to mind, to my knowledge. But I believe that you could look at how phpMyAdmin does it and tweak that to your needs, since you seem to like how it does it. Just make sure your usage of it is within the license agreement of phpMyAdmin.
Brad F Jacobs
The answer is right in spirit, but you really should HTMLSPECIALCHARS the SQL string before you feed it back to the browser. You don't want to spend too much time on the problem; after all it's much more important to eliminate the errors than make them pretty. Only make them pretty enough to where you can interpret them, figure out what's wrong, and fix them.
Ian
Ian, this will be purely for debugging purposes so there's no necessity to htmlescape the output as it's your own data. I'm awarding the bounty to this answer as it will expire anyway, but I'm not marking it as answered - it's not.
Raveren
+1  A: 

Maybe off topic, but I do not think that the most common errors during development are SQL errors. As displaying the errors in generally I have xDebug installed and it handles the "fine" tunning of error messages.

mhitza
this should have been a comment to the question, not an answer.
Raveren
Agree 100%. SQL errors are relatively easy to avoid, and no production applications should even have the possibility of erroring out.. Do your query development in a database app (Such as `phpMyAdmin`, `MySQL Administrator` or even the `mysql` command line application...
ircmaxell
@Raveren I didn't have enough points to comment at that moment.
mhitza
+2  A: 

I'm not sure whether this would work..

All the error messages from mysql are rendered from this file only share/errmsg.txt

Error message information is listed in the share/errmsg.txt file. %d and %s represent numbers and strings, respectively, that are substituted into the Message values when they are displayed.

A sample error message would be , %s near '%s' at line %d

so if we can add something in all the error messages at errmsg.txt , then we can check for the matching string in the string returned by php mysql_error function then make them bold or italic..

I think there is no specific class wrote before..

You should go from scratch and make it to your own server.

kvijayhari
Hey, thanks man, that's really handy, wish I could grant the bounty to you, but it's too late.
Raveren
I'm glad that answer helped you.. not bothered about the bounty.. :)
kvijayhari
A: 

Try http://krumo.sourceforge.net/ from the website:

To put it simply, Krumo is a replacement for print_r() and var_dump(). By definition Krumo is a debugging tool (initially for PHP4/PHP5, now for PHP5 only), which displays structured information about any PHP variable.

Eduardo Romero
I use it and extend it constantly, it's actually very uninformative and uncomfortable by default, displays little to none available info about objects and resource variables and still uses lots of PHP4 features. When I'm done with the changes I might share the code if its license permits it.
Raveren