PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > function I created doesn't work
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
function I created doesn't work

Réponse
 
LinkBack Outils de la discussion
Vieux 04/01/2008, 17h06   #1
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 .

-afan
  Réponse avec citation
Vieux 04/01/2008, 17h28   #2
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

Daniel Brown wrote:
> On Jan 4, 2008 12:06 PM, afan pasalic <afan@afan.net> wrote:
>> 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 .

>
> 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).
>

not quite sure I understand?!?

  Réponse avec citation
Vieux 04/01/2008, 17h52   #3
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

Daniel Brown wrote:
> On Jan 4, 2008 12:28 PM, afan pasalic <afan@afan.net> wrote:
>> Daniel Brown wrote:
>>> On Jan 4, 2008 12:06 PM, afan pasalic <afan@afan.net> wrote:
>>>> 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 .
>>> 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).
>>>

>> not quite sure I understand?!?
>>
>>
>>

>
> The fundamentals of PHP (and general programming): working with globals.
>
> Specifically for PHP, some required reading:
>
> http://us.php.net/global
>
>

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
  Réponse avec citation
Vieux 04/01/2008, 18h04   #4
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

Samuel Vogel wrote:
> Explanation of your code:
>
> $CONTENT = get_content(12, 104, 'merchant');
> echo $CONTENT;
>
> This does not work, because you don't use a "return" in your function.
> This means that the function does not return a value. Now in the
> function you assign a value to $CONTENT. That works, as you pointed out
> with the second example.
> But after that the line above sets $CONTENT to the empty return value of
> the function. And therefore it is empty!
>
> so long,
> Samy
>


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
  Réponse avec citation
Vieux 04/01/2008, 18h19   #5
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

Daniel Brown wrote:
> On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
>> 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).

>
> 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).
>


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
  Réponse avec citation
Vieux 04/01/2008, 18h24   #6
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

On Jan 4, 2008 12:06 PM, afan pasalic <afan@afan.net> wrote:
> 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 .


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.
  Réponse avec citation
Vieux 04/01/2008, 18h34   #7
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

On Jan 4, 2008 12:28 PM, afan pasalic <afan@afan.net> wrote:
>
> Daniel Brown wrote:
> > On Jan 4, 2008 12:06 PM, afan pasalic <afan@afan.net> wrote:
> >> 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 .

> >
> > 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).
> >

> not quite sure I understand?!?
>
>
>


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.
  Réponse avec citation
Vieux 04/01/2008, 18h42   #8
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work [SOLVED]

Daniel Brown wrote:
> On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
>> 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).

>
> 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).
>

Actually, there were 2 misstakes:
<?php
function get_InfoContent($instance_id, $form_id, $InfoKey)
{
$query = mysql_query("
SELECT instance_id, form_id, InfoContent
FROM forms_single_info
WHERE instance_id=".$instance_id." AND form_id=".$form_id." AND
InfoKey='".$InfoKey."'
");
if (mysql_num_rows($query) > 0)
{
$result = mysql_fetch_assoc($query);
$InfoContent = $result['InfoContent'];
return $InfoContent;
}
else
{
$InfoContent = get_InfoContent(0, 0, $InfoKey);
return $InfoContent;
}

}
?>
Yes, I ALSO need "return" in "else" statement because when I call the
function 2nd time the first return will return to else and then the 2nd
return will return to main code.


And, I called the function 2nd time with
get_InfoContent(0, 0, $InfoKey);
and it should be
$InfoContent = get_InfoContent(0, 0, $InfoKey);




-afan
  Réponse avec citation
Vieux 04/01/2008, 19h02   #9
afan pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

Jim Lucas wrote:
> afan pasalic wrote:
>> Daniel Brown wrote:
>>> On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
>>>> 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).
>>> 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).
>>>

>> 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
>>

>
> Trust me, you have to return in the else part, otherwise it isn't going to work!
>


yup. you're right. didn't understand WHY I needed it there. now I got it.


thanks jim.

-afan
  Réponse avec citation
Vieux 04/01/2008, 19h03   #10
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

afan pasalic wrote:
> 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);


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.

> }
> }
>
>
> get_content(12, 104, 'merchant');
> echo $CONTENT; # Shows correct.
>
> What's wrong with first solution?
>
> Thanks for any .
>
> -afan
>



--
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
  Réponse avec citation
Vieux 04/01/2008, 19h08   #11
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
> 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).
>


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.
  Réponse avec citation
Vieux 04/01/2008, 19h17   #12
Daniel Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
> 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).


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.
  Réponse avec citation
Vieux 04/01/2008, 19h52   #13
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] function I created doesn't work

afan pasalic wrote:
> Daniel Brown wrote:
>> On Jan 4, 2008 12:52 PM, afan pasalic <afan@afan.net> wrote:
>>> 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).

>> 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).
>>

>
> 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
>


Trust me, you have to return in the else part, otherwise it isn't going to work!


--
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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26320 seconds with 21 queries