I'm looking to make some improvements to a deliberately crude API written in PHP, it's designed to handle simple requests and talk to the DB.
I have the following DB class:
class DB
{
public function connect()
{
$serverName = ".";
$connectionInfo = array("Database"=>"Demo","UID"=>"sa","PWD"=>"qwe123");
$connection = sqlsrv_connect( $serverName, $connectionInfo);
return $connection;
}
public function close($connection)
{
return sqlsrv_close($connection);
}
public function query($connection, $query, $params = null)
{
return sqlsrv_query($connection, $query, $params);
}
}
And then for requests I do:
$db = new DB();
$conn = $db->connect();
$sql = "INSERT INTO Applications (name, friendly_name, description, enabled, visible, icon, video) VALUES (?, ?, ?, ?, ?, ?, ?)";
$params = array($name, $friendly_name, $description, $enabled, $visible, $icon, $video);
$query = $db->query($conn, $sql, $params);
if( $query === false )
{
$response = array('error' => 'Error');
}
else
{
$response = array('success' => 'Success');
}
$db->close($conn);
return $response;
I was wondering how I could improve this further and not have to repeat the db connect and creating an instance of the class and close the connection.
I know I could and should be using PDO, but for this example I want to keep it old school.