While loop only grabs first row from table for RSS feed

For some reason this while loop only fetches the 1st row of the table for an RSS feed. Not sure what is missing…

$output = '';
$now = date('Y-m-d H:i:s');
$mysqli = new mysqli('dbhost', 'username', 'pw', 'dbname');
if($stmt = $mysqli->prepare("SELECT 
                                id,
                                etitle,
                                elocation,
                                edesc,
                                estartdate,
                                eenddate
                              FROM modx_migx_events
                              WHERE
                                published=1")){
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($id, $etitle, $elocation, $edesc, $estartdate, $eenddate);
  
  while($stmt->fetch()){
    if($eenddate >= $now){
      $edesc = strip_tags($edesc);
      $edesc_trimmed = strlen($edesc) > 600 ? substr($edesc,0,600).'...' : $edesc;
      $estartdate_formatted = date("D, d M Y H:i:s O", strtotime($estartdate));
      $output .= '<item>
                    <title>'.$etitle.'</title>
                    <description>'.$elocation.' - '.$edesc_trimmed.'
                    '.$estartdate_formatted.'</description>
                    <guid isPermaLink="false">http://www.ultimusfundsolutions.com/[[~26? &objectID=`[[+id]]`</guid>
                    <dc:creator>Ultimus Admin</dc:creator>
                  </item>';
      }//end if endddate >= now
  }//end while
  $stmt->close();
  $mysqli->close();
 return $output;
}//end if

what does fetch_all() give you? Did you have a look at the Database (e.g. with PHPmyAdmin) if the statement gives you more than one result? You can also try PDO.

While you’re at it, wouldn’t it be easier to have the query only retrieve events where the enddate column is later than the current date, rather than adding it as a condition in your PHP code?

Turns out is was an xml error that was preventing the loop to grab all published rows.

Corrected it to this:

$output .= '<item>
                    <title>'.$etitle.'</title>
                    <link>https://ultimusfundsolutions.com?id=26</link>
                    <description>'.$elocation.' - '.$edesc_trimmed.'</description>
                    <pubDate>'.$estartdate_formatted.'</pubDate>
                    <guid isPermaLink="false">http://www.ultimusfundsolutions.com/events#event'.$id.'</guid>
                    <dc:creator>Ultimus Admin</dc:creator>
                  </item>';

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.