【发布时间】:2015-06-11 22:06:39
【问题描述】:
我在 PHP 中的以下 SQL 查询不能完全工作。结果仅包含第一行。该查询在 PHPMyadmin 中运行良好,它会返回所有结果。
$select = "SELECT a.setID, a.setName, a.setPrimaryLanguage, a.setSecondaryLanguage
FROM Person_Set ps, Album a
WHERE ps.Person_username = :username
AND ps.Set_setID = a.setID";
try {
$stmt = $dbh->prepare($select, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$stmt->bindValue(":username", $username, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch();
echo json_encode($result);
unset($stmt);
} catch (Exception $e) {
echo 'Exception : ' . $e->getMessage() . "\n";
}
此外,如果我更改选择条件以搜索包含特定字符串的行,则结果为空(返回“false”)。如下查询:
$select = "SELECT a.setID, a.setName, a.setPrimaryLanguage, a.setSecondaryLanguage
FROM Album a, Set_Card s
WHERE a.setName LIKE '%:searchText%'
AND a.setID = s.Set_setID
GROUP BY a.setID";
我一直在尝试不同的方式来连接 MySQL 并获得结果,比如
$results = $mysqli->query($query);
而不是使用 PDO。但是,结果仍然相同。谁能帮忙指出我的错误在哪里?非常感谢!
【问题讨论】:
-
尝试
$result = $stmt->fetchAll();而不是$result = $stmt->fetch(); -
另外,命名占位符
'%:searchText%'不需要加引号