将列结果存储在变量中

时间:2014-12-12 18:07:16

标签: php mysql pdo alias

我使用以下SQL语句获取了列(我使用的是PDO):

$query = 'SELECT people.firstname,people.lastname,makes.make,cars.price
          FROM MyCars cars
          JOIN AllUsers people ON user.id = cars.userID
          JOIN AllMakes makes ON make.id = cars.makeID
          ORDER BY cars.price ASC LIMIT 0,1';

$statement = $this->_dbHandle->prepare($query);
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);

我尝试将已检索到的某些列存储到变量中,例如将AllUsers表中的'firstname'列存储到变量'$ firstname'中这是可能的吗?

1 个答案:

答案 0 :(得分:1)

您的函数返回一个关联数组,其中每列都在一个元素中,其键是列名。所以如果你这样做:

$row = yourfunc();

然后你可以这样做:

$firstname = $row['firstname'];