Q:

SQLSTATE[HY000] General SQL Server error: Check messages from the SQL Server. (severity 5)

I have this error and tried various solution over stack over flow and internet sites but to no avail. I'm stuck since I can't think of anyway how to solve it.
Here's my code which generates the said error.
<?php
session_start();
$name = $_REQUEST['name'];

require 'config.php';

try {

    $db = new PDO($dsn, $username, $password);
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $sth = $db->query("SELECT * FROM locations where name = '$name'");

    $locations = $sth->fetchAll();

     echo json_encode( $locations );
} catch (Exception $e) {
    echo $e->getMessage();
}
//header("Location: index.html?platemobnum=$platemobnum");

config.php
<?php

 $server     = "XXXXX";
 $username   = "XXX";
 $password   = "XXXXX";
 $database   = "Database";

 $dsn        = "mssql:host=$server;dbname=$database";

A:

The following code will be valid, assuming your webserver can communicate with your database server and the credentials are correct. It uses a PDOStatement to prevent SQL injections.
session_start();

require 'config.php';

try {
    // create a new connection (verify that your web server can communicate with db server and validate credentials)
    $db = new PDO( $dsn, $username, $password );

    // prepare a statement to prevent SQL injections
    $stmt = $db->prepare( "SELECT * FROM locations WHERE name = ?" );

    /* populate the arguments in your prepared statement.
       needs to be an array even though there is only one argument. */
    $stmt->execute( array( $_REQUEST['name'] ) );

    // fetch all results
    $locations = $stmt->fetchAll();

    // encode as JSON
    echo json_encode( $locations );

} catch (Exception $e) {
    echo $e->getMessage();
}

