I need to perform a simply query.
Literally, all I need to perform is:
SELECT price, sqft, zipcode FROM homes WHERE home_id = X
When I use PHP PDO, which I've read is the recommended way to connect to a MySQL database, simply creating the connection takes a measured 610ms.
My code is below:
try {
$conn_str = DB . ':host=' . DB_HOST . ';dbname=' . DB_NAME;
$dbh = new PDO($conn_str, DB_USERNAME, DB_PASSWORD);
$params = array();
$sql = 'SELECT price, sqft, zipcode FROM homes WHERE home_id = :home_id';
$params[':home_id'] = X;
$stmt = $dbh->prepare($sql);
$stmt->execute($params);
$result_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
// json output
ob_start("ob_gzhandler");
header('Content-type: text/javascript; charset=utf-8');
print "{'homes' : ";
print json_encode( $result_set );
print '}';
ob_end_flush();
$dbh = null;
} catch (PDOException $e) {
die('Unable to connect');
}
Question: What's the fastest way for me to connect to my MySQL database to perform the query above?