2015-04-07 92 views
0

我在PHP中的以下SQL查詢無法完全工作。結果只包含第一行。該查詢在PHPMyadmin內完全正常工作,它將所有結果返回給我。此外,如果我更改選擇標準以搜索包含某些字符串的行,則結果爲空(返回'false')。下面查詢:PHP MySQL查詢不返回所有結果

$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的。但是,結果仍然相同。任何人都可以幫助指出我的錯誤在哪裏?非常感謝你!

+0

嘗試'$結果喜= $ stmt-> fetchAll();'而不是'$ result = $ stmt-> fetch();' –

+0

另外,命名佔位符''%:searchText%''不需要引用 – Ghost

回答

1

PDOStatement::fetch獲取一行,並移動指針到下一行獲取的所有記錄。您將使用$results = $stmt->fetchAll()檢索所有結果或像這樣的循環:

while ($result = $stmt->fetch()) { 
    echo json_encode($result); 
} 
1

您使用取的獲取只有一排,而不是使用此代碼()函數,

$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"; 
    }