尝试通过 php5-fpm 5.4.4-14 查询 MySQL 5.5.30 服务器时失败。这是我们要查询的内容:
mysql> select * from cat where cat = 'sc';
+------+------+--------------------+
| ID | cat | name |
+------+------+--------------------+
| 1 | sc | SOLID CARBIDE BITS |
+------+------+--------------------+
1 row in set (0.00 sec)
这里有一个类似的查询,首先尝试获取类别名称,然后是一个指定其他字段的数组。和print_r($result);
不echo $result['name'];
返回结果。尝试使用 PDO 语句查询 MySQL 数据库时,我忽略了什么?
<?php
$pdo = new PDO('mysql:host=localhost;dbname=database','username','password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$cat= $_GET["cat"];
/* let try grabbing one of those rows, do not think an array should be here? */
$stmt = $pdo->prepare('SELECT * FROM cat WHERE cat = :cat');
$stmt->bindParam(':cat', $cat);
/* based on the feedback below, we must execute */
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
echo $result['name'];
?>