guymclarenza Posted January 7, 2021 Share Posted January 7, 2021 I have been staring at this code for hours trying to figure out what is wrong, I guarantee that someone is going to look at it and make me feel like a tool. The first query is not going through the list, the second is. It worked till I started updating to PDO. I have tested the SQL, that works, probably a flipping comma or a quotation mark screwing with my brain. Please look and spot the obvious balls up and point it out to me. as in I get a result that looks like this Heading question question question and it stops. I am expecting Heading question question question Heading question question question Why is this failing? Please show a doddering old fool what I am doing wrong... $query = $pdo->prepare("SELECT * FROM faq_cats WHERE faqc_site = :site ORDER BY faqc_id ASC LIMIT 0,4"); $query->bindParam(":site", $site); $query->execute(); while($row = $query->fetch(PDO::FETCH_ASSOC)) { $fid = $row["faqc_id"]; $faqc = $row["faqc_name"]; echo "<h2> ".$faqc." </h2>"; $query = $pdo->prepare("SELECT * FROM faqs WHERE faq_cat = :fid ORDER BY faq_id ASC"); $query->bindParam(":fid", $fid); $query->execute(); while($row1 = $query->fetch(PDO::FETCH_ASSOC)) { $faid=$row1["faq_id"]; $faqn=$row1["faq_question"]; echo "<a href=\"#".$faid."\">".$faqn."</a><br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/311983-this-is-a-pain-in-the-bottom-everything-should-do-as-is/ Share on other sites More sharing options...
Solution Barand Posted January 7, 2021 Solution Share Posted January 7, 2021 Probably because the inner loop's result object is overwriting the outer result object as you store them both in $query. But don't run queries inside loops like that . Use a single query with a join to get all the data in a single query. It's far more efficient. SELECT faqc_name , faq_id , faq_question FROM faq_cats c JOIN faqs f ON f.faq_cat = c.faqc_id WHERE faqc_site = :site ORDER BY faqc_id , faq_id 1 Quote Link to comment https://forums.phpfreaks.com/topic/311983-this-is-a-pain-in-the-bottom-everything-should-do-as-is/#findComment-1583699 Share on other sites More sharing options...
guymclarenza Posted January 7, 2021 Author Share Posted January 7, 2021 Thank you, I knew it would be something obvious. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311983-this-is-a-pain-in-the-bottom-everything-should-do-as-is/#findComment-1583700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.