Jump to content

This is a pain in the bottom, everything should do as is.


Go to solution Solved by Barand,

Recommended Posts

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

 

  • Solution

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

 

  • Great Answer 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.