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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.