Jump to content

Recommended Posts

I have apiece of code to retireve ads placed by a selected user from a db.

The code use a function to retieve the info and then goes through the result to dispaly the records in a table.

I am getting an error I have not sen before. Can anyone tell me what it means.

 

The error is

....uninitialized string offset:0

This occurs in both cols.

The code is

//set short variables
				$u=$_SESSION['user_id'];

				$ads=get_ads($u);


				if (is_array($ads)) 
		{
			echo "<h2>You have the following advertisements pending activation</h2><br/>";
			echo '<table id="t4" align="center">';
			 echo '<caption align="center"><b>Advertisement Summary<b></caption>';
  			
  				echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>';
        
        		foreach ($ads as $row)
        		{
				echo '<tr><td>';
     
 			echo ($row['ad_ref']);

 			echo '</td>';

 			echo '<td>';
     
 			echo ($row['date_placed']);

			 echo '</td></tr>';

			}
			 echo '</table>' ;

			include ('./includes/footer.html'); 
			  
			  
		}
		else{
			echo "You have no advertisements awaiting activation";
			include ('./includes/footer.html');
			exit();
		}

The function is
[code]
function get_ads($u)
{
				//connect to db
				$connection = db_connect();  

       				
       			//retrieve all ads placed by user but not activated.
       			
			$query="SELECT * FROM ads WHERE user_id='$u'";

			$result= mysql_query($query)or die(mysql_error());
			if (!$result)
     				return false;
  					$result = mysql_fetch_assoc($result);
   					return $result;
}	       	       

 

[/code]

Link to comment
https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/
Share on other sites

It would be nice if you could tell us where the error occurs? Which line does the error message tell you it happens on? And do you think you could point that line out to us? It'd make it a whole stack easier to help you...

 

Edit: Hmm, i guess i wasn't paying all that much attention - you did sort of say where it happens. I think, you need to change the way your function works slightly. You'll need to pass the result resource back to the script, and then use mysql_fetch_assoc() function in the main script:

 

<?php
function get_ads($u)
{
				//connect to db
				$connection = db_connect();  

       				
       			//retrieve all ads placed by user but not activated.
       			
			$query="SELECT * FROM ads WHERE user_id='$u'";

			$result= mysql_query($query)or die(mysql_error());
			$num = mysql_num_rows($result);
                if($num == 0){
                    return false
                }else{
                    return $result
                }
}
?>

				$u=$_SESSION['user_id'];

				$ads=get_ads($u);


				if ($ads)
		{
			echo "<h2>You have the following advertisements pending activation</h2><br/>";
			echo '<table id="t4" align="center">';
			echo '<caption align="center"><b>Advertisement Summary<b></caption>';
  			
  				echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>';
        
        		while($row = mysql_fetch_assoc($ads))
        		{
				echo '<tr><td>';
     
			echo ($row['ad_ref']);

			echo '</td>';

			echo '<td>';
     
			echo ($row['date_placed']);

			echo '</td></tr>';

			}
			echo '</table>' ;

			include ('./includes/footer.html'); 
			  
			  
		}
		else{
			echo "You have no advertisements awaiting activation";
			include ('./includes/footer.html');
			exit();
		}		

Yes sorry.

The error occurs on line 49 and 55 marked below:

 

$u=$_SESSION['user_id'];

				$ads=get_ads($u);


				if (is_array($ads)) 
		{
			echo "<h2>You have the following advertisements pending activation</h2><br/>";
			echo '<table id="t4" align="center">';
			 echo '<caption align="center"><b>Advertisement Summary<b></caption>';
  			
  				echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>';
        
        		foreach ($ads as $row)
        		{
				echo '<tr><td>';
     
 			echo ($row['ad_ref']);//line 49 error

 			echo '</td>';

 			echo '<td>';
     
 			echo ($row['date_placed']);//line 55 error

			 echo '</td></tr>';

			}
			 echo '</table>' ;

			include ('./includes/footer.html'); 
			  
			  
		}
		else{
			echo "You have no advertisements awaiting activation";
			include ('./includes/footer.html');
			exit();
		}


 

thanks for that, however now get a parse error in the function at the else condition. I cant see why?

function get_ads($u)
{
				//connect to db
				$connection = db_connect();  

        			//retrieve all ads placed by user but not activated.
       			
			$query="SELECT * FROM ads WHERE user_id='$u'";

			$result= mysql_query($query)or die(mysql_error());
			$num = mysql_num_rows($result);
                if($num == 0)
			{
                    return false
                }else{       //parse error
                    return $result
                }
}	       	     

Whoops. Forgot some semi colons. And i can't even blame that on it being tired and late - its midday!

 

Try:

function get_ads($u)
{
				//connect to db
				$connection = db_connect();  

        			//retrieve all ads placed by user but not activated.
       			
			$query="SELECT * FROM ads WHERE user_id='$u'";

			$result= mysql_query($query)or die(mysql_error());
			$num = mysql_num_rows($result);
                if($num == 0)
			{
                    return false;
                }else{       //parse error
                    return $result;
                }
}

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.