2

As I try to consolidate my code and make it more available to other projects, I've run into a problem: variables that were generated and available are not anymore when that routine is moved to a function: This is the query:

$count = "SELECT eid, Count, Name, name2, Email, pay FROM h2018";

THIS WORKS FINE:

$result = $mysqli->query($count);
$row = $result->fetch_assoc();
foreach($row as $key=>$value){
    $a = $key;
    $$key = $value;
    echo($a." and ".$value."<BR>");
}

NOT WORKING FINE:

function avar($result) {
$row = $result->fetch_assoc();
    foreach($row as $key=>$value){
        $a = $key;
        $$key = $value;
    }
}


$result = $mysqli->query($count);
avar($result);
echo($a." and ".$value."<BR>");

I thought the variable variables would be available from outside of the function. I tried doing a return, but that didn't help. I also tried to global $$key, but that didn't work either. What am I doing wrong?

sukebe7
  • 69
  • 6
  • what is your purpose of `$$key = $value;` ? – kuromoka Oct 09 '18 at 06:46
  • The problem is the variables created are only in scope for the function, once that exits they will disappear. Not sure why you want to do this as the common practice would be to just use the values in the array returned by `fetch_assoc()`. – Nigel Ren Oct 09 '18 at 06:50
  • The purpose is to create variables with values added automatically, without having to dive into such common redundancy and chances to introduce bugs. This particular routine is used to generate an automatic table based on the query alone. – sukebe7 Oct 09 '18 at 10:18

2 Answers2

0

There is multiple mistakes or missing steps like return or array

function avar($result) {
$data=array();
$row = $result->fetch_assoc();
    foreach($row as $key=>$value){
        $a = $key;
        $data[$key] = $value;//if there is multiple records then used array otherwise you should used variable 
    }
return $data;
}


$result = $mysqli->query($count);
$data=avar($result);//get return value
print_r($data);
Bilal Ahmed
  • 3,698
  • 3
  • 19
  • 38
  • You may be better off using `$data[$key] = $value;` as this associates the value with the field being set. – Nigel Ren Oct 09 '18 at 06:57
  • Because this routine generates variable variables, I don't see the need for an array... or return. However, the $data[$key] = $value; is a nice suggestion. – sukebe7 Oct 09 '18 at 10:16
  • actually, $data[$key] = $value; does not replace – sukebe7 Oct 10 '18 at 12:53
0

Please, read the PHP documentation about the Variable Scope for more information. The variables inside your function are local so you cannot access them outside of your function. You would have to return something.

For example, this could work:

function avar($result) {
    $resultArray = false;
    $row = $result->fetch_assoc();

    foreach ($row as $key => $value) {
        $resultArray = [
            'key' => $key,
            'value' => $value
        ];
    }

    return $resultArray;
}


$result = $mysqli->query($count);
$queryResult = avar($result);

if ($queryResult) {
    echo 'Key: ' . $queryResult['key'] . ' | Value: ' . $queryResult['value'];
}

Please do note that fetch_assoc will return an array with multiple items if there is more than one result. In my example only one (and the last) result will be returned.

Edit: As @Nigel Ren said in his comment. In this case you're basically rebuilding an array which is going to look (nearly) the same as the array which is being returned by fetch_assoc, which is pointless. My function can be used if you want to add conditions in the future, or manipulate some data. Otherwise, don't use a function and just use the results from fetch_assoc.

Stefan R
  • 322
  • 2
  • 6
  • 26
  • I did read through the scope section. I was trying to figure out how to globalize the variable variables ($$key), but global $$key didn't work. I'll do more research and trial and error. – sukebe7 Oct 09 '18 at 12:19