Search code examples
phpmysqlsqlflash-builder

OOPHP calling a function within a function


OK so I am using OOPHP to communicate between Flash Builder and MYSQL both functions work but when i call one of the functions within the other it throws an error

ERROR: Server error MySQL Error - 2014 I have search the Internet and cant find any suitable answers.

Is it because the first function has a lock on the MYSQL database and so the second cant access the database?

The reason i have this problem is in one Table i have the clients country as initials and i have created a second table with what the initials stand for i.e. UK = United Kingdom, i did this as at the time i guess i thought this would be good database practice to save space if the client table went to many records.

So now i am calling a function that gives me a particular client but within that call i wanted to call the second function to change the country initials into the correct country string.

FIRST FUNCTION

public function getClients($item) {

    $stmt = mysqli_prepare($this->connection, "SELECT timestamp, id, fname, lname, dateofbirth, monthofbirth, yearofbirth,
    town, country, sex, rate, comments from $this->tablename WHERE (id = $item)");  
    $this->throwExceptionOnError();

    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();

    $rows = array();

    mysqli_stmt_bind_result($stmt, $row->timestamp, $row->ID, $row->fname, $row->lname, $row->dateofbirth, $row->monthofbirth, 
    $row->yearofbirth, $row->sex, $row->country, $row->town, $row->rate, $row->comments);

    while (mysqli_stmt_fetch($stmt)) {
      $row->fname = ucfirst(substr($row->fname,0,1));
      $row->lname = ucfirst($row->lname);
      $row->town = ucfirst($row->town);
      $row->comments = strip_tags($row->comments);
      $row->lname = (($row->fname) . " " . ($row->lname));
      $row->country = $this->getCountry($row->country);
      $row->yearofbirth = GetAge($row->dateofbirth. '-' .$row->monthofbirth. '-' .$row->yearofbirth);
      $row->Pic_loc = "";
      $row->Pic_loc= "SLAGSIMAGES/".($row->ID)."/image01.jpg";
      $row->timestamp = new DateTime($row->timestamp);
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->timestamp, $row->id,$row->fname,$row->lname,$row->sex,$row->country,$row->town,$row->dateofbirth,
      $row->monthofbirth,$row->yearofbirth, $row->rate, $row->comments);
    }


    mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);



    return $rows;
}

And this is the second function that is called by the first

public function getCountry($countrycode) {

    $stmt = mysqli_prepare($this->connection, "SELECT country FROM countrycodes where CountryCodes ='$countrycode'");       
    $this->throwExceptionOnError();

    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();

    $rows = array();

    mysqli_stmt_bind_result($stmt, $row->country);

    while (mysqli_stmt_fetch($stmt)) {
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->country);
    }

    mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}   

This is the piece of code in the first function that is throwing the error $row->country = $this->getCountry($row->country);

Sorry if code is a little confusing but thanks in advance for any correct answer or info to tell my brain why it can't be done.


Solution

  • "So now I am calling a function that gives me a particular client but within that call I wanted to call the second function to change the country initials into the correct country string"

    Why not just join to the other SQL table in the first function, get the data you need and not have to call a second function?