我需要执行一个简单的查询.
原文链接:https://www.f2er.com/php/136443.html从字面上看,我需要做的就是:
SELECT price,sqft,zipcode FROM homes WHERE home_id = X
当我使用PHP PDO时,我已经阅读了推荐的连接MysqL数据库的方法,只需创建连接就需要610毫秒.
我的代码如下:
try { $conn_str = DB . ':host=' . DB_HOST . ';dbname=' . DB_NAME; $dbh = new PDO($conn_str,DB_USERNAME,DB_PASSWORD); $params = array(); $sql = 'SELECT price,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'); }