a1anm
a1anm

Reputation: 1629

Beginner PHP Question - Access Data from Function

I have this function:

function selectValue($test) {
    $connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM table where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);
}

selectValue('abcde');

echo $value['something'];

This results in $value becoming an array. I would like to access this array from outside of the function. I tried to do this using the last line of code above (ie. echo...) but this doesn't work. How should I do this?

Upvotes: 0

Views: 160

Answers (7)

Mike
Mike

Reputation: 21659

You need to return a value from your function:

function selectValue($test) {
    $connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM pans where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);
    return $value;
}


$value = selectValue('abcde');

echo $value['something'];

Be aware that the function could fail at various places, so you should not always assume that the return value will contain the result from mysql_fetch_assoc.

One way to do that (one way of many) is to test the return value before using it:

function selectValue($test) {
    $value = false;
    $connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM pans where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);
    return $value;
}    

if($value = selectValue('abcde')) {
    echo $value['something'];
} else {
    echo "Something went wrong.\n";
}

Exception handling is another way to handle errors.

Upvotes: 7

helle
helle

Reputation: 11660

return the value

return $value

or use globals. but globals are not state of the art anymore and shouldn't be used.

Upvotes: 0

Luke
Luke

Reputation: 3353

function selectValue($test) {
$connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM table where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);
return $value;
}


$value = selectValue('abcde');

echo $value['something'];

Upvotes: 4

xil3
xil3

Reputation: 16439

You can return value from the function - here I rewrote it for you:

function selectValue($test) {
    $connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM pans where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);

    return $value;
}


$val = selectValue('abcde');

echo $val['something'];

Upvotes: 2

David Radcliffe
David Radcliffe

Reputation: 1491

I assume you have already defined $value outside that function? Otherwise you need to return it so you can access it outside the function.

Upvotes: 0

Simiil
Simiil

Reputation: 2311

This may help

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?> 

Upvotes: -2

Sarfraz
Sarfraz

Reputation: 382806

Put the return keyword in your function:

function selectValue($test) {
$connection = dbConnect(HOST, USERNAME, PASSWORD, DATABASE);
    $query = "SELECT * FROM pans where value = '$test'";
    $results = @mysql_query($query, $connection);   
    $value = mysql_fetch_assoc($results);
    return $value; // return the value
}

Upvotes: 2

Related Questions