problem with return in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
peppelorum
Forum Newbie
Posts: 12
Joined: Sun Jul 21, 2002 3:19 pm
Location: Leksand, Sweden

problem with return in a function

Post by peppelorum »

Having trouble to return a value from a function. Just before I try to return I print out the variable to see if it contains anything, and it does that every time. Calls the function with $thread = back($row[0]);
Anyone with a clue?

Code: Select all

function back($id)
{

	global $db;
	
	$sql = "SELECT eforum_reply,eforum_id ".
	"FROM eforum ".
	"WHERE eforum_id = ".$id."";
	
	$result = mysql_query($sql,$db);
	if( mysql_error() ) { die(mysql_error());}
	
	while($row2 = mysql_fetch_row($result))
	{
	
		if($row2ї0] != 0) back($row2ї0]);
		else $tagg = $row2ї1];
	
	}
	
	echo($tagg);
	return $tagg;

}
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Since it echos a value for you in your function then it's your query string.

You're concatenation is a little funky.

Try this:

Code: Select all

$sql = "SELECT eforum_reply, eforum_id FROM eforum WHERE eforum_id = ". $id;
How do you have register globals set in you php.ini? Looks like it's set to on. if it's set to off, and it should be, you need to access the value via the super global arrays $_GET, $_POST for php 4.2.0 ++.
Or $HTTP_GET_VARS, $HTTP_POST_VARS for pre 4.2.0


"Quote the constants and concatenate the variables"
User avatar
peppelorum
Forum Newbie
Posts: 12
Joined: Sun Jul 21, 2002 3:19 pm
Location: Leksand, Sweden

Post by peppelorum »

yes I know the " " on the end of sql-string was strange:) but no It doesnt work what you said.

since I send $id to the function when I calls it means that register globals is not involved here, echos the $id in the beginning of the function and it is always correct.
User avatar
peppelorum
Forum Newbie
Posts: 12
Joined: Sun Jul 21, 2002 3:19 pm
Location: Leksand, Sweden

Post by peppelorum »

have found that it´s the while that makes the trouble.. is it possible to check the value in the db without a while?
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Put this in your while loop.


Code: Select all

echo "Value of $row&#1111;0] is: <br>" . $row&#1111;0];
And what do you see?
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

try !== instead of !=
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

fatalcure wrote:try !== instead of !=
It's not !== it is !=

so is that a not not :lol:
User avatar
peppelorum
Forum Newbie
Posts: 12
Joined: Sun Jul 21, 2002 3:19 pm
Location: Leksand, Sweden

Post by peppelorum »

solved it by changing in the while, but thanks for the effort:)

Code: Select all

while($row2 = mysql_fetch_row($result)) 
&#123; 
  
   if($row2&#1111;0] != 0) $tagg = back($row2&#1111;0]); 
   else $tagg = $row2&#1111;1]; 
    
&#125;
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

i coulda sworn it was !== and not != ... maybe u can use both?
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Code: Select all

$sentiment = "I hate recursive functions!";
while( earth revolves around the sun )
    &#123;
          echo $sentiment . "<br>\n";
    &#125;
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

!== means, more or less, 'exactly not equals to', while != means 'not equals to'

$var = 5;

if($var != true)
//doesn't get here: 5 is 'equal to' true

if($var !== true)
//gets here: 5 is not 'exactly equal to' true (because they're
//not of the same type; boolean vs. integer)

check out the manual for a better description
Post Reply