Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > function I created doesn't work
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 8th November 03:50
afan
External User
 
Posts: 1
Default function I created doesn't work



hi
I have function
function get_content($client_id, $form_id, $index1)
{
$query = mysql_query("
SELECT content
FROM infos
WHERE client_id=".$client_id." AND form_id=".$form_id." AND
index1='".$index1."'");
if (mysql_num_rows($query) > 0)
{
$result = mysql_fetch_assoc($query);
return $result['content'];
}
else
{
get_content(0, 0, $index1); // get default value
}
}

When I call it
$CONTENT = get_content(12, 104, 'merchant');
echo $CONTENT; // empty, nothing

But if I use global in the function

function get_content($client_id, $form_id, $index1)
{
global $CONTENT;
$query = mysql_query("
SELECT content
FROM infos
WHERE client_id=".$client_id." AND form_id=".$form_id." AND
index1='".$index1."'");
if (mysql_num_rows($query) > 0)
{
$result = mysql_fetch_assoc($query);
$CONTENT = $result['content'];
}
else
{
get_content(0, 0, $index1);
}
}


get_content(12, 104, 'merchant');
echo $CONTENT; # Shows correct.

What's wrong with first solution?

Thanks for any help.

-afan
  Reply With Quote


 


2 8th November 03:50
parasane
External User
 
Posts: 1
Default function I created doesn't work



Functions only use variables within their own scope, unless
explicitly told to consider a variable as a global (or if the variable
is a SUPERGLOBAL).

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
3 8th November 03:50
afan
External User
 
Posts: 1
Default function I created doesn't work


not quite sure I understand?!?

  Reply With Quote
4 8th November 03:50
parasane
External User
 
Posts: 1
Default function I created doesn't work


The fundamentals of PHP (and general programming): working with globals.

Specifically for PHP, some required reading:

http://us.php.net/global


--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
5 8th November 03:50
lists
External User
 
Posts: 1
Default function I created doesn't work


return get_content(0, 0, $index1);
you need to return the results of the second call.

When you hit the second call, you are not returning the results.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Reply With Quote
6 8th November 03:50
afan
External User
 
Posts: 1
Default function I created doesn't work


Sorry for confusing with 2nd function. Let's take it out, forget about it.

function get_content($client_id, $form_id, $index1)
{
$query = mysql_query("
SELECT content
FROM infos
WHERE client_id=".$client_id." AND form_id=".$form_id." AND index1='".$index1."'");
if (mysql_num_rows($query) > 0)
{
$result = mysql_fetch_assoc($query);
return $result['content'];
}
else
{
get_content(0, 0, $index1); // get default value
}
}

$CONTENT = get_content(12, 104, 'merchant');
echo $CONTENT; // empty, nothing

There is "return", right after $result = mysql_fetch_assoc($query);

Some additional info (hopefully will not confuse again ) I'm pulling
content from table infos for specific client, form and index1. If there
is no record I'm using recursive part (inside "else") to get the default
value (client_id=0, form_id=0).
When echo the content right before "return" I can see it. But can't see
it in echo after calling the function?!?!

thanks

-afan
  Reply With Quote
7 8th November 03:50
parasane
External User
 
Posts: 1
Default function I created doesn't work


You're right, I read it too quickly. My apologies.

Off the top of my head, they look syntactically similar, and the
end result should be the same. Try adding some error displaying to
the script to see if anything jumps out at you.

<?

error_reporting("E_ALL");
// Include your database connection routines here.

function get_content($client_id, $form_id, $index1)
{
$query = mysql_query("
SELECT content
FROM infos
WHERE client_id=".$client_id." AND
form_id=".$form_id." AND index1='".$index1."'") or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
$result = mysql_fetch_assoc($query);
return $result['content'];
}
else
{
get_content(0, 0, $index1); // get default value
}
}

echo get_content(12, 104, 'merchant');
?>

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
8 8th November 03:50
afan
External User
 
Posts: 1
Default function I created doesn't work


I think you didn't understand my question: I know why the function work
in 2nd example. My question was why I'm not getting the result in 1st
example? What am I doing wrong. And, as far as I know, I think it
doesn't have anything with GLOBALS (register_globals are anyway turned off).

thanks

-afan
  Reply With Quote
9 8th November 03:50
parasane
External User
 
Posts: 1
Default function I created doesn't work


Also, keep in mind that, in the else{} clause of the first
function, you're not using return; to send back the information. In
my opinion, you shouldn't call a function from within its own
definition because it can cause a loop if the conditions are met and
the else{} clause is reached over and over again. If there is a
situation where get_content(0, 0, $index1); doesn't return any rows,
the function will loop eternally (that is, until PHP gets dizzy and
gives up).

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
10 8th November 03:50
afan
External User
 
Posts: 1
Default function I created doesn't work


that's "recursive" function and it can call itself (though, you're
right, if you are not careful you can finish in loop ).
and I think I don't need return in "else" statement because the result
to be send "back" is in "if" statement.

-afan
  Reply With Quote
Reply


Thread Tools
Display Modes




666