我試圖通過php5-fpm 5.4.4-14查詢MySQL 5.5.30服務器時失敗。下面是我們正在嘗試查詢:PDO查詢不返回結果
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'];
?>
大,它允許'的print_r($結果);'返回陣列的'的陣列([0] =>數組([ID] => 2 [cat] => scs [name] => SOLID CARBIDE SPIRAL BITS))'',儘管我無法解析echo $ result ['name'];'的單個元素,是否有更好的方法返回這樣的查詢? – Astron
做fetchOne()代替 –