Home:ALL Converter>Unable to use variable declared in a function inside another function

Unable to use variable declared in a function inside another function

Ask Time:2017-01-24T10:48:26         Author:bigbaddevil7

Json Formatter

Didn't know how to ask this question.

I was working on setting up class to handle my Database interaction and have a weird interaction of passing variables. I must be missing something.

I have a class DataBaseAPI I have a function QueryDB($value) that will be called by other functions. But it has an issue with my declaring that $value inside another function. Example:

Works

include_once('DataBaseAPI.php');
$DB = new DataBaseAPI();

$test = $DB->QueryDB('id');
echo $test;

Doesn't Work

include_once('DataBaseAPI.php');
$DB = new DataBaseAPI();

$test = $DB->getId();
echo $test;

Start of the class in DataBaseAPI.php

class DataBaseAPI {


  public function getId(){
    //the same function defined the same way but needing to use $this
    $this->QueryDB('id');//seems like a waste here but is for ease of use later on
  }


  public function QueryDB($value){
    echo $value; //echo's id
    global $conn; 
    $token = '7ci4f8ykfik3ndzufksy1dp16x3na4'; //Test Token not a real token
    $doesExists = "SELECT $value FROM user_info WHERE token='$token'";
    $existsResult = mysqli_query($conn, $doesExists);
    $check = $existsResult->fetch_assoc();
    return $check[$value];
  }
} 

I even checked with an echo the $value in the QueryDB($value) echo's id the same way as when I call the function directly.

I just don't understand why the first method works yet the second method doesn't, yet I'm still calling it the same way. Just inside another function.

Author:bigbaddevil7,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41819192/unable-to-use-variable-declared-in-a-function-inside-another-function
yy