PHP MySQL 查询未返回所有结果
PHP MySQL query not returning all results
我在 PHP 中的以下 SQL 查询无法完全正常工作。结果仅包含第一行。查询在 PHPMyadmin 中完全正常,returns 我得到了所有结果。
$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。但是,结果仍然相同。谁能帮忙指出我的错误在哪里?非常感谢!
PDOStatement::fetch — Fetches the next row from a result set
因此,当您只进行提取时,它会提取第一行,除非您使用循环将光标更改为下一条记录。
您可以使用fetchAll
方法获取所有记录
PDOStatement::fetch 获取单行并将指针移动到下一行。您将使用 $results = $stmt->fetchAll()
检索所有结果或像这样的循环:
while ($result = $stmt->fetch()) {
echo json_encode($result);
}
您好,您正在使用 fetch() 函数,它只获取一行而不是使用此代码,
$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->fetchall();
echo json_encode($result);
unset($stmt);
} catch (Exception $e) {
echo 'Exception : ' . $e->getMessage() . "\n";
}
我在 PHP 中的以下 SQL 查询无法完全正常工作。结果仅包含第一行。查询在 PHPMyadmin 中完全正常,returns 我得到了所有结果。
$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。但是,结果仍然相同。谁能帮忙指出我的错误在哪里?非常感谢!
PDOStatement::fetch — Fetches the next row from a result set
因此,当您只进行提取时,它会提取第一行,除非您使用循环将光标更改为下一条记录。
您可以使用fetchAll
方法获取所有记录
PDOStatement::fetch 获取单行并将指针移动到下一行。您将使用 $results = $stmt->fetchAll()
检索所有结果或像这样的循环:
while ($result = $stmt->fetch()) {
echo json_encode($result);
}
您好,您正在使用 fetch() 函数,它只获取一行而不是使用此代码,
$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->fetchall();
echo json_encode($result);
unset($stmt);
} catch (Exception $e) {
echo 'Exception : ' . $e->getMessage() . "\n";
}