Jump to content

[SOLVED] if statement not working in a while loop


ballhogjoni

Recommended Posts

I have a while loop that loops through the resource of the mysql query and I am testing whether there are results or not, but for some reason this doesn't work. The code should echo No results were found becasue there are no results in the db right now. If I print_r() the $rResult variable I get the resource id #5 error. I was looking to see if you can help me or give me some advice. Here is my code:

 

<?php
$oDB = new FACSDatabase();
$rResult = $oDB->Select('Articles', '*');
while ( $row = mysql_fetch_array( $rResult ) ) {
    if( $row['title'] == '' || $row['title'] == NULL ){
	 echo '<tr><td colspan="2">No results were found</td></tr>';
	}else{
	 echo '<tr><td>'.$row['title'].'</td><td><a style="font-size:10px;" href="?id='.$row['article_id'].'&action=edit">[edit]</a></td></tr>';
	}
   }
?>

 

Select() in class FACSDatabase

<?php
function Select($sTable=NULL,$a_sData=NULL,$sWhere=NULL,$sGroupBy=NULL){
	if( is_array( $a_sData ) ){
		$sColumns = "";
		if( sizeof( $a_sData ) > 1 ){
			foreach( $a_sData as $data => $value ){
				$sColumns .= $value.',';
			}
			$a_sData = trim( $sColumns, ',' );
		}else{
			foreach( $a_sData as $data => $value ){
				$sColumns .= $value;
			}
			$a_sData = $sColumns;
		}
		$sql = "SELECT ".$a_sData." FROM ".$sTable." ".$sWhere." ".$sGroupBy;
		$aResult = mysql_query($sql) or die( "MySql Error: ".mysql_error() );
	}else{
		$sql = "SELECT ".$a_sData." FROM ".$sTable." ".$sWhere." ".$sGroupBy;
		$aResult = mysql_query($sql) or die( "MySql Error: ".mysql_error() );
	}
	return $aResult;
}
?>

Link to comment
Share on other sites

Do the echo here and see if it is any different:

<?php
$oDB = new FACSDatabase();
$rResult = $oDB->Select('Articles', '*');
while ( $row = mysql_fetch_array( $rResult ) ) {
                 print_r($row);
    if( $row['title'] == '' || $row['title'] == NULL ){
	 echo '<tr><td colspan="2">No results were found</td></tr>';
	}else{
	 echo '<tr><td>'.$row['title'].'</td><td><a style="font-size:10px;" href="?id='.$row['article_id'].'&action=edit">[edit]</a></td></tr>';
	}
   }
?>

Link to comment
Share on other sites

Sorry, I didn't see where your "No results" echo was, earlier. It is never getting to that condition because there are no rows for it to ever start the while loop. Try this:

<?php
$oDB = new FACSDatabase();
$rResult = $oDB->Select('Articles', '*');
if( mysql_num_rows($rResult)
   echo '<tr><td colspan="2">No results were found</td></tr>';
while ( $row = mysql_fetch_array( $rResult ) ) {
	 echo '<tr><td>'.$row['title'].'</td><td><a style="font-size:10px;" href="?id='.$row['article_id'].'&action=edit">[edit]</a></td></tr>';
   }
?>

Link to comment
Share on other sites

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.