V.Dimitrov V.Dimitrov - 1 year ago 95
MySQL Question

Return value of UsersModel::myFunc() must be of the type string, object returned

I get this "Fatal error" when I call this function:

public function myFunc(int $id) : string
$statement = self::$db->prepare("Some query WHERE id = ?");
$statement->bind_param("i", $id);
$result = $statement->get_result();
return $result;

Can you give me advice where can I read for this types of errors? Thanks for your time.

Answer Source

Change it like that (if you use MYSQLi):

    #SELECT field1 FROM xyz where id = ?
    $result = $statement->get_result();
    $data = $result->fetch_row();
    return $data['field1']?$data['field1']:'';

Note on Fatal Error the funtion has an ReturnTypeHint string so you have to return a string. Read here: http://php.net/manual/en/class.typeerror.php and here: http://php.net/manual/de/functions.returning-values.php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download