Jump to content

function help


dk4210

Recommended Posts

Hello Guys,

 

I am trying to understand how to return a value from a function. I just can't get it to work.

 

For example I have a page lets call it index.php and on this page I have a call to a function like this

$scat = 2;

// Function to choose which query to use		
    what_Table($scat,$w_query);

  echo "This is the w query" $w_query;

 

I have an included file called general_functions.php and on that page I have this code

function what_Table($scat,$w_query){

$ctquery = mysql_query("SELECT w_query FROM ad_category WHERE cat_status='1' AND cat_id='$scat'")
    or die(mysql_error());  
    $row = mysql_fetch_array($ctquery);
    
    // Stands for which query
    $w_query = $row['w_query'];
       
    return $w_query;

 

The echo $w_query does not return a value. Any help would be appreciated!

Thanks, Dan

 

Link to comment
https://forums.phpfreaks.com/topic/231381-function-help/
Share on other sites

From what I can see, your code only has a few minor errors.

 

echo "This is the w query" $w_query;

Wont work, you'll need to add the two strings together with a .

 

Like this:

echo "This is the w query" . $w_query;

 

Also, to get a return value, you just do:

function example($i) {
$value = $i + 1;
return $value;
}

 

You do not need to add the return variable to the parameter list. In your example you call "what_Table($scat,$w_query);" without having anything in the $w_query variable, this will give a Notice: Undefined variable:  error.

So when you call the function, do it like this:

$w_query = what_Table($scat);

 

and remove the $w_query from the parameter list.

Then it should work just fine and also the echo should work.

 

I hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190793
Share on other sites

Hmm Still not working for me. Within the function I get a 1(which is correct) out side the function I get nothing.

 

 

index.php page (Echo just to test it)


    // Function to choose which query to use		
    what_Table($scat);

   $w_query = what_Table($scat);
   
   echo" This is outside the function" . $w_query;

exit;	

 

 

The function [echo just to test it out] 

/ Choosing cat type function. This is for differnt queries.
function what_Table($scat){

$ctquery = mysql_query("SELECT w_query FROM ad_category WHERE cat_status='1' AND cat_id='$scat'")
    or die(mysql_error());  
    $row = mysql_fetch_array($ctquery);
    
    // Stands for which query
    $w_query = $row['w_query'];
       
       Echo "<br>This is inside the function" . $w_query; 

Here is the result..
http://screencast.com/t/T2voSWe0zDja

Thanks for all your help thus far..I just can't seem to get it working correctly..

Link to comment
https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190939
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.