Why is this undefined?..I FIXED IT YES!!!

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

User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..help appreciated

Post by califdon »

scarface222 wrote:Thanks for the suggestion. I echoed the mysql statement and received resource id#3 and as for passing a variable through the argument what exactly do you mean. Do you mean writing the post statement within the function brackets?
No. Functions often require arguments, as yours do. That means that when you call the function, you must supply values to it. In the definition of the function, the expected arguments are listed within the parentheses following the name of the function, on the first line. When you call the function, you must supply values for those arguments within the parentheses of the calling statement. Your get_message() function requires 3 arguments, your get_content() function requires 2 arguments. I am saying that you should require the 3rd argument in get_content(), just like you do in get_message, that 3rd argument being the topic_id. Then, in both functions, you should NOT redefine the argument by referring to the $_POST array, because you already defined it as an argument to the function. Of course, you will need to refer to the $_POST array to get the proper value to use as an argument, before trying to call the function.

You were not echoing the SQL statement if it echo'ed "Resource id#3". That could only come as a result of trying to echo the mysql query results, which cannot be printed out except by fetching the rows and echoing individual column values. The SQL statement begins with "SELECT" or "INSERT" or "UPDATE", etc. That's what you want to echo back, so you know exactly what you are asking the database to do, since that's what is failing. I suspect that when you do that, you will find that some values are missing, which then explains why you get no valid results on which mysql_fetch can operate. But you'll never know until you look at the SQL statement to see what it really contains.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined?..help appreciated

Post by scarface222 »

Thanks again for the response Califdon and for hanging in there with me. I am still learning and appreciate the help. When I use the $topic_id as an argument in the GetContent function (getContent($link, $num, $topic_id)), '$res = getContent($link, 500);' on line 59 of the full code that I submitted runs into problems since it is missing the argument and when I add the variable in that argument it is undefined. When I echoed the mysql statement $topic_id comes up as blank. As for the function argument what is the best way to approach this? Do the arguments within the parenthesis just define the variables that will be in the function? Can you give me an example of the right way to fix these problems. It would help me see the problems more clearly. Thanks again for the lessons man.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..help appreciated

Post by califdon »

scarface222 wrote:Thanks again for the response Califdon and for hanging in there with me. I am still learning and appreciate the help.
You're welcome.
When I use the $topic_id as an argument in the GetContent function (getContent($link, $num, $topic_id)), '$res = getContent($link, 500);' on line 59 of the full code that I submitted runs into problems since it is missing the argument and when I add the variable in that argument it is undefined. When I echoed the mysql statement $topic_id comes up as blank.
Exactly. That's what you needed to discover, that you are missing a value, which explains why the query failed, which explains why the mysql_fetch produced the error that it didn't have a valid resource.
As for the function argument what is the best way to approach this? Do the arguments within the parenthesis just define the variables that will be in the function?
They define the variables that are passed to the function, as well as their values, at the time it is called from somewhere in the program. You can define additional variables within the function, but of course they will have the same value every time the function is called.
Can you give me an example of the right way to fix these problems. It would help me see the problems more clearly. Thanks again for the lessons man.
I think you just need to learn more about what functions are and how they are used. Here's a pretty good short tutorial: http://www.webcheatsheet.com/PHP/functions.php
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined?..help appreciated

Post by scarface222 »

I understand for the most part how functions work now, thank you for that califdon. This is however kind of confusing still though because I checked the value of $topic_id for the insertmessage function where I post $topic_id and when it inserts into the database it inserts the value I want however for some reason it is undefined in the previous getcontent function where the same steps are taken. Why could this be?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..help appreciated

Post by califdon »

scarface222 wrote:I understand for the most part how functions work now, thank you for that califdon. This is however kind of confusing still though because I checked the value of $topic_id for the insertmessage function where I post $topic_id and when it inserts into the database it inserts the value I want however for some reason it is undefined in the previous getcontent function where the same steps are taken. Why could this be?
Have you removed the lines that assign a value to $topic_id INSIDE your functions? In case you haven't, that would explain that. If that's not the problem, you just need to trace its value at each step and find out where it is not defined and correct that. Remember that any variable defined INSIDE a function only has meaning WITHIN that function, even if it has the same name as a global variable that was declared outside the function. That's why you should not define variables inside functions unless they are used ONLY inside that function.

Code: Select all

+------------------------------------------+
| // MAIN PROGRAM                          |
|   ...                                    |
|   $foo = 3;                              |
|   ...                                    |
|   echo "The value of foo is ".bar($foo); |
|   ...                                    |
|                                          |
|   +------------------------------+       |
|   | function bar($insidefoo) {   |       |
|   |    // do something           |       |
|   |    return $insidefoo;        |       |
|   | }                            |       |
|   +------------------------------+       |
+------------------------------------------+
will echo "3" because that's the VALUE that was passed into the function. In the above example, the name of the inside variable is different than the global variable. If I had used the same variable name ($foo) inside the function, it would work the same, but where I said "do something", if I had said $foo=4, then it would echo "4" because although the value of $foo didn't change (outside the function), the value of bar($foo) DID change. Actually, it is possible to "pass by reference", where a global variable's value can be changed from within a function, but that is less commonly done and I don't want to confuse you at this point.

It's very important to fully understand how functions work, to use PHP.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined?..help appreciated

Post by scarface222 »

This is making more and more sense to me now...I deleted both variable definitions and attempted to use a global definition outside the function but for some reason it is undefined! I do not know why this is because then why would it only be defined in the insertmessage function where it inserts the correct value into the database when tested. I double checked to make sure the variable was getting passed and it definitely is but only inside that function when I use the post method and then define it like you said not to within the function. When I define it outside the functions using post $topic_id=$_POST['topic_id']; I just get the error undefined index. How could this be?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..help appreciated

Post by califdon »

It can't be. You're misinterpreting something. First, make sure of the sequence of steps within your script. It is easy to make the mistake of trying to use a variable before it has been given a value. Be sure that the sequence is:
  1. Assign a value to the variable
  2. Call the function, using the variable's value (now assigned) as an argument
  3. Do whatever you need to do within the function, using the value that was passed as an argument
  4. Return whatever value you are expecting from the function.
Make sure you understand what a function does: it may receive one or more values (or none) as arguments to be used within the function, then it returns a single value. It's the same as the built-in functions, like substr(), which requires at least 2 arguments and an optional 3rd argument, and returns a string which is a substring of the first argument value, beginning at an offset specified by the 2nd argument value. Your user defined functions work exactly the same way.

[Edit: I should emphasize that it is the VALUE that is passed in an argument, NOT the variable itself (unless you use a different syntax to force it to pass by reference, which I'm avoiding in this discussion). If that doesn't make sense to you, think about it some more, it's a very important point.]

A word of advice: avoid repeating something you know is wrong, even if it seems like it's working for you. That practice will lead to utter confusion, because you are reinforcing something that is wrong! Try to learn the correct process or syntax and if you need to, seek help, as you are doing, but by all means don't continue doing something that you have reason to know is just incorrect. For instance, don't keep defining a new local variable inside the function, that has the same name as a totally different global variable. That road leads to disaster.

The better way to resolve your problem is to set aside your script, for now, and write a new script, a very short one that contains nothing but a process similar to the one that's causing you trouble. Without all the surrounding complexity, it is usually much easier to see what's going on, then you can apply your understanding to your problem script. Something like this:

Code: Select all

<?php
function process($y) {
   return "The value you passed was " . $y;
}
 
if(isset($_GET['x']) {
   $x = $_GET['x'];
} else {
   $x = "MISSING!";
}
$z = process($x);
echo $z;
?>
If you save that in a file named foobar.php, then run it from your browser by addressing localhost/foobar.php?x=hello, you should see "The value you passed was hello".
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined?..help appreciated

Post by scarface222 »

No you're right as usual and I think I finally understand how functions work after tinkering and your lessons however there is something weird going on with this script and I think I should have mentioned this much sooner but I am not using the standard method of posting to the script which would be directly through a form. The form goes through a javascript POST function which may explain the irregular undefined behavior. Even still I am positive that somehow the variables are getting passed because I was still able to get them into the database but only with one function. For example

Code: Select all

insertMessage($_POST['user'], $_POST['message'], $_POST['topic_id']);
This works with the function set up like so

Code: Select all

function insertMessage($user, $message, $topic_id){
    
    $query = sprintf("INSERT INTO s(user, message, topic_id) VALUES('%s', '%s', '$topic_id');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
    $res = @mysql_query($query);
    if(!$res)
        die("Error: ".mysql_error());
    else
        return $res;
}
And this does not work with a message of an undefined variable

Code: Select all

$res = getContent($link, 500, $_POST['topic_id']);
with the function set up like so

Code: Select all

function getContent($link, $num, $topic_id){
        $res = @mysql_query("SELECT date, user, message FROM s WHERE topic_id='$topic_id' ORDER BY date DESC LIMIT ".$num, $link);
    if(!$res)
        die("Error: ".mysql_error());
    else
        return $res;
}
Additionally I though I should metion that the $_POST variables of message and user are undefined outside of their function call. Could this help explain anything?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..help appreciated

Post by califdon »

You're confusing yourself and you're confusing me by not getting to the heart of the problem. FIND OUT WHAT THE VALUES ARE AT EACH STEP. All this other discussion is useless. Just put some echo statements to show the values of the variables--before you use them in function calls, inside the function, before using them in an SQL statement, then the whole SQL statement. Programming is an exact science--things don't "just happen". Everything occurs because of what you instruct it to do. You will drive yourself crazy if you continue to think of it as something mysterious that "seems" to do something you don't want it to do. FIND OUT WHAT'S WRONG. You know that the problem is being caused by something being undefined. Then DETERMINE WHY IT'S UNDEFINED. The way you do that is to trace the value by echoing it to the screen with some identifying language, at each point in your program, until you find out where it is no longer correct. That will show you exactly where it's going astray.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined? I SOVLED THIS YES!!!!

Post by scarface222 »

OH MY GOD I FIXED IT!!!!!!!!!!!!!!!!!!! HAHA I am so HAPPY. If I didn't think I would get banned or something I would so be spamming some f-bombs followed by "yeah" in celebration :drunk: . It was the javascript that made the variables undefined because I had only one function specifically receiving the posted variables which was specified in the javascript and I forgot to set the same arrangement for the other one. That's why no matter what was suggested for php it didn't work because the variables were undefined outside the functions. I highly appreciate all the feedback from everyone especially Califdon who hung in there with me and taught me some new things in a very patient and informative manner until noticeably irritated with my noob responses at the very end after like 15 tutorials. I also want to thank my family and friends for their love and support and of course uhh... god! haha just joking...Hopefully I won't ever have to post such an epic topic again... :banghead:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..I FIXED IT YES!!!

Post by califdon »

:lol: I'm really glad you got it fixed, and especially that you figured out the logic. I wasn't really irritated, although perhaps impatient, but I really did want YOU to solve it yourself, which you did! It's a good feeling, isn't it?

Oops! Power outage here! My UPS keeps my computer running for a few minutes, but I gotta sign off now. Second time tonight! Just finished resetting all my clocks and now I gotta do it again! :evil: This heat wave, I guess.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Why is this undefined?..I FIXED IT YES!!!

Post by scarface222 »

Thanks again bro. Damn right it feels good after all that frustrating work, but at least I learned some new stuff. As far as I am concerned you make this forum a better place :wink:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why is this undefined?..I FIXED IT YES!!!

Post by califdon »

See, that makes it worth my time! I appreciate that.
Post Reply