L
L
lexstile2021-04-07 17:05:18
PHP
lexstile, 2021-04-07 17:05:18

Why is PDO returning a string instead of a number?

There is a method:

public function getUser($user_id) {
    $params = [
      'user_id' => $user_id,
    ];
    return $this->db->row('SELECT * FROM users WHERE id = :user_id', $params);
  }

  public function query($sql, $params = []) {
    $stmt = $this->db->prepare($sql);
    if (!empty($params)) {
      foreach ($params as $key => $val) {
        $stmt->bindValue(':'.$key, $val, (is_int($val) ? PDO::PARAM_INT : PDO::PARAM_STR));
      }
    }
    $stmt->execute();
    return $stmt;
  }

  public function row($sql, $params = []) {
    $result = $this->query($sql, $params);
    return $result->fetchAll(PDO::FETCH_ASSOC);
  }


Why is getUser returning all data as a string, whether it's a number or a string?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2021-04-07
@lexstile

There are two options here: either you have a DBMS that stores data in the form of strings (sqlite for example), or you have the PDO::ATTR_EMULATE_PREPARES option enabled (set to true).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question