0

I have what it must be a very simple method, but it's unexpectedly diying at 'return' line and not throwing any errors. I already enabled error reporting E_All; I already checked apache error_log;

What am I missing?

public function get_by_state_id(int $state_id){
    $db = new mysqli('localhost', 'root', '', 'foo');
    $query = "
        SELECT *
        FROM {$this->table}
        WHERE state_id = ?
        ORDER BY name";
    if($stmt = $db->prepare($query)){
        $stmt->bind_param('i',$state_id);
        $stmt->execute();
        $res = $stmt->get_result();

        $rows = [];

        while($obj = $res->fetch_object()){
            $rows[] = $obj;
        }

        return $rows;
    }
}
Uriel
  • 352
  • 5
  • 17
  • 2
    How do you know it's dying and not just returning anything? Add an else statement for when the prepare fails, and return something else, or just return an empty array. – aynber Jun 10 '19 at 17:55
  • 1
    Have you tried this: [How to enable MySQLi exception mode?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 10 '19 at 17:55
  • @anyber I know it cause I'm debugging and I see that the array has values – Uriel Jun 10 '19 at 18:13
  • @Dharman I had not, but now I did it and still not getting any message :( – Uriel Jun 10 '19 at 18:18
  • If you are debugging, where you able to check the values of `$rows` in the debugger? Have you noticed any pattern or anything special about it? – Dharman Jun 10 '19 at 18:20
  • @Dharman Nothing special. It just save the values into $rows as if evereyhing is about to be just fine, but then it dies. – Uriel Jun 10 '19 at 18:24
  • Would you be able to prepare a minimal set of data from that data for which it dumps. I would like to try to reproduce this on my machine. You can use `var_export()` – Dharman Jun 10 '19 at 18:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194716/discussion-between-uriel-and-dharman). – Uriel Jun 10 '19 at 18:29
  • How are you call this function? – AbraCadaver Jun 10 '19 at 18:37

1 Answers1

0

As @Dharman point me out: the error was due to be echoing unscaped utf8 chars.

It was solved adding this right after the mysql connection:

$db->set_charset('utf8mb4');

He also provided a link to read more about it: UTF-8 all the way through

Uriel
  • 352
  • 5
  • 17