简体   繁体   中英

While loop only displaying first result

So, I had this code working earlier today--and all of a sudden it decided to only start displaying the first result from the query. I cannot figure out what i've changed since then, I actually believe that I haven't changed anything... anyway... I've gone into the DB and altered the table so that all the "upgrades" meet the requirements to be displayed, and yet still only one result is being shown.

        $sql = "SELECT id, name, cost, count(*) FROM upgrades 
            WHERE id NOT IN (Select upgrade_id  FROM thehave8_site1.user_upgrades WHERE uid = :uid)
            AND nullif NOT IN(SELECT upgrade_id FROM thehave8_site1.user_upgrades WHERE uid = :uid2)
            AND prereq IN (SELECT upgrade_id FROM thehave8_site1.user_upgrades WHERE uid = :uid3)
            ;";
    $que = $this->db->prepare($sql);
    #$que->bindParam(':id', $id); //note the : before id
    #$que->bindParam(':id2', $id);
    $que->bindParam(':uid', $this->uid);
    $que->bindParam(':uid2', $this->uid);
    $que->bindParam(':uid3', $this->uid);
    try { 
        $que->execute();
        while($row = $que->fetch(PDO::FETCH_BOTH))
        {

                echo "<div class='upgrade' id={$row[0]}><p>{$row[1]}</p><p>{$row[2]}</p></div>";

        }
    } catch(PDOException $e) { echo $e->getMessage();}

Aren't you missing some line of code that advances the result in the query to the next row? When I do loops through recordsets (slightly different than what you are doing but probably not much different) there is usually a MoveNext or something like that - I see nothing like that here.

I don't know this language you are using.

......

I am not being allowed to add a comment to your response so I will add it here.. .... Cool. You really looked like you knew what you were doing, far more advanced than anything I've written! New here also and didn't catch onto the tag system, thanks for pointing that out so I don't need to embarrass myself in future. Glad you've solved it. I think Count (*) would work if you included it as a subquery

SELECT FIELD1, FIELD2, (SELECT count (*) FROM ... ) AS FIELD3

FROM (ETC)

or if you just want its value in the recordset result, compute it ahead of time via query, and then include its value as a dummy/constant field in your select statement. Depending on the query plan in your query engine this may or may not be more efficient.

Or just wait to get Recordcount from your recordset.

问题已解决,尽管我不确定确切原因,但查询字符串末尾的count(*)阻止了整个代码正常运行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM