[英]PHP MySQL query not returning all results
My following SQL query in PHP does not work fully. 我在PHP中执行以下SQL查询无法完全正常工作。 The result only contains the first row.
结果仅包含第一行。 The query works totally fine inside PHPMyadmin, which returns me all the results.
该查询在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";
}
Besides, if I change the selection criteria to search for rows containing certain string, the result is empty (returned 'false'). 此外,如果我更改选择标准以搜索包含某些字符串的行,则结果为空(返回“ false”)。 Query below:
查询如下:
$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";
I have been trying different ways to connect to MySQL and get the results, like 我一直在尝试不同的方法来连接到MySQL并获取结果,例如
$results = $mysqli->query($query);
instead of using PDO. 而不是使用PDO。 However, the results are still the same.
但是,结果仍然相同。 Could anyone help to point out where my mistakes are?
谁能指出我的错误在哪里? Thank you very much!
非常感谢你!
PDOStatement::fetch — Fetches the next row from a result set
PDOStatement :: fetch —从结果集中获取下一行
So when you just do a fetch it fetches the first row, unless you do it using a loop which changes the cursor to the next record. 因此,当您仅执行获取操作时,它会获取第一行,除非您使用将光标更改为下一条记录的循环来执行此操作。
You may get all the records using fetchAll
method 您可以使用
fetchAll
方法获取所有记录
http://php.net/manual/en/pdostatement.fetch.php http://php.net/manual/en/pdostatement.fetch.php
http://php.net/manual/en/pdostatement.fetchall.php http://php.net/manual/en/pdostatement.fetchall.php
PDOStatement::fetch fetches a single row and moves pointer to the next row. PDOStatement :: fetch提取一行并将指针移至下一行。 You will either use
$results = $stmt->fetchAll()
to retrieve all results or a loop like this: 您将使用
$results = $stmt->fetchAll()
检索所有结果,或者使用如下所示的循环:
while ($result = $stmt->fetch()) {
echo json_encode($result);
}
Hi you are using fetch() function which fetch only one row instead use this code, 嗨,您正在使用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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.