Drav'
Drav'

Reputation: 105

Interpret PDO errors with jQuery after an AJAX query

I'm writing a little install script for my current project. The first "step" of this script asks the user for his MySQL credentials and tests them. Everything is done via an AJAX query using jQuery's $.ajax.

My client-side script looks like this:

      <section class="col-md-6" style="display: none;" id="pane2">
            <h1 style="text-align: center;">MySQL Setup</h1>
            <form method="POST" action="stuff.php" role="form" class="installform">
                <input type="text" class="form-control" placeholder="Host" name="host" id="host">
                <input type="text" class="form-control" placeholder="Database Name" name="dbname" id="dbname">
                <input type="text" class="form-control" placeholder="Username" name="sqlusername" id="sqlusername">
                <input type="password" class="form-control" placeholder="Password" name="sqlpassword" id="sqlpassword">
            </form>
            <a><button class="btn btn-primary btn-lg btn-block" id="sqlbutton">Next</button></a>
            <script>
                $(function() {
                    $('#sqlbutton').click(function() {
                        var host = $('#host').val();
                        var dbname = $('#dbname').val();
                        var username = $('#sqlusername').val();
                        var password = $('#sqlpassword').val();

                        var credentials = "host=" + host + "&dbname=" + dbname + "&username=" + username + "&password=" + password;
                        $.ajax({
                            type: "POST",
                            url: "sqlchecker.php",
                            data: credentials,
                            success: function(data) {
                                if (data == 'success') {
                                    $('#sqlbutton').html("All right.");
                                }

                                else {
                                    $('#sqlbutton').html("Something is wrong");
                                }
                            }
                        });
                    });
                });
            </script>

And my sqlchecker.php script is currently this:

<?php
    $host = $_POST['host'];
    $dbname = $_POST['dbname'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

    // Opens a connection to the database using PDO
    try {
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);

    }

    catch(PDOException $ex)
    {
        die("Couldn't connect to the database. Error code: " . $ex->getMessage());
    }

    echo('success');
?>

Of course, at the moment, it makes absolutely no distinction between errors, if there wasn't any, it displays "All right." and if there was one, then it displays "Something is wrong". But I'd like it to be a bit more specific and tell the user what exactly is wrong in the query.

I'm looking to kind of "parse" PDO exceptions, the problem is that they return a complete sentence that is almost impossible to use in an if statement. Is there a way to configure PDO at the connection so it only returns an error code, that would be way easier to interpret using jQuery?

Thanks a lot for your answers!

Upvotes: 0

Views: 1015

Answers (1)

Steve B
Steve B

Reputation: 644

A call to getCode on the exception should give you a code you can work with. You could then switch on this and give your user some appropriate feedback. This would definitely be cleaner than trying to manipulate the string message returned.

$errorCode = $ex->getCode();
switch($errorCode) {
    case 1000:
        //...
        break;
    default:
        //...
        break;
}

For my sql I believe the error code will be listed here:

https://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html

Upvotes: 2

Related Questions