Function confusion

Hi All!

I’m getting confused with a SIMPLE php function that supposed to work BUT I simply couldn’t figure out what went wrong…


function add_point($table_field, $point_value, $receiver) {
			
	global $q_add_point;

	$q_add_point = "UPDATE points SET '$table_field' = ".$point_value." WHERE username = ".$receiver."";

	if (mysql_query($q_add_point)) {
		return true;
		}
	else { return false; }
	
	echo $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables
	
} // end function new point

if (add_point('contact', 15, $asker_username)) {
echo "<h1>ACTIVITY POINT UPDATED!</h1>";
} // end add point OK
else { 
	echo "ERRROR : ".mysql_error();
	echo $q_add_point;
	}

Why are the passed variables ($table_field, $point_value AND $receiver) ALL EMPTY??? I tried to echo them both in function and then outside the function (making them global) but they are all empty… :mad:
Plz point me to my mistake, I can’t see even a syntax error anymore…

THNX!

Put the echo as first instruction in the function. Right now it never gets executed because FALSE or TRUE is returned before.

I did that but still, all 3 variables are EMPTY, NOTHING, NICHTS…

And are you sure the function is being called?

Definitely, YES!

And how do you know? It might seem a dumb question, but you should see those variables echoed (at least if you put that echo statement at the beginning), so checking some dumb things could be a good start :wink:

for debugging purposes, I would do this first

 
 
[COLOR=#007700]function [/COLOR][COLOR=#0000bb]add_point[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$table_field[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$point_value[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$receiver[/COLOR][COLOR=#007700]) { 
             
    echo 'got here<br />';[/COLOR]
[COLOR=#007700]     echo [COLOR=#0000bb]$table_field[/COLOR][COLOR=#007700].[/COLOR][COLOR=#dd0000]"<br />"[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]$point_value[/COLOR][COLOR=#007700].[/COLOR][COLOR=#dd0000]"<br />"[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]$receiver[/COLOR][COLOR=#007700];[/COLOR]
      die();[/COLOR]

then at least ‘got here’ should appear on the screen, if nothing else, if the function is actually called.

I tested your code with the echo at the top of the function, and this is the result:


contact
15

ERRROR : Can't connect to local MySQL server through socket '/...' (2)UPDATE points SET 'contact' = 15 WHERE username = 

So the variables aren’t empty. Only the last one, because you send a variable there and in the code you posted you don’t give it a value anywhere.
Everything works just fine as far as I’m concerned.

Thank you guys for trying to help me!
I added :


echo "My variables : <br />".$table_field."<br />".$point_value."<br />".$receiver;

The function IS called : 'My variables : ’ is echoed and the query is echoed :


UPDATE points SET '' = WHERE username =

As you can see, there’s NOTHING where the variables should be… Like they are empty string, number or whatever.

Beats me. As I posted just before you, it works fine for me.
Are you sure there isn’t some other call in your code to that function where you use all variables instead of fixed values, and those variables don’t have a value?

That confused me even MORE!!! :lol:
The third variable I didn’t posted in the script here but it HAS a value.
How can the script ‘work’ at your side??? Maybe it’s an Apache issue?

When your function assigns the sql string to $q_add_point you are expecting that updated value to be available from outside of the function. That isn’t working as you expect.

I suggest that you update the function so that instead of returning true, it returns the string instead. Then you can use


if (q_add_point = add_point(..., ..., ...)) {
    ...
}

try:


function add_point($table_field, $point_value, $receiver) { 
             

    $q_add_point = "UPDATE points SET '$table_field' = ".$point_value." WHERE username = ".$receiver.""; 

   if (mysql_query($q_add_point)) { 
        echo 'TRUE: ' . $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables 
    } else { 
        echo 'FALSE: ' . $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables 
        return $q_add_point; //so it can be echoed......
    } 
     
    
     
} // end function new point 
$asker_username = 'Spike';
if (add_point('contact', 15, $asker_username)) { 
    echo "<h1>ACTIVITY POINT UPDATED!</h1>"; 
} // end add point OK 
else {  
    echo "ERRROR : ".mysql_error(); 
    echo $q_add_point; 
    } 

Do a test with a very simple script


<?php

function add_point($table_field, $point_value, $receiver) {
    return "My variables<br />$table_field<br />$point_value<br />$receiver";
} 

$username = 'username';
echo add_point('contact', 15, $username);

?>

And see what happens. Does this work?

WORKED!!!

Thank YOU ALL guys for your time and help!!!

No worries :slight_smile:

Well, beats me if I understand why those variables would be empty in the OP’s script and filled in yours, spike… :shifty: