|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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?!? ![]() |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 pullingcontent 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|