0

I want to check if a member id from a database , is an integer , but when i use is_int function on this data it return me false.. primary key should be an int no?

$query = $sql->prepare("SELECT * FROM `blog` WHERE `categorie` = 'Dossier' ORDER BY `date_ajout` DESC LIMIT  " . $offset . "," . $page_result . "   ");
$query->bindValue(':offset', $offset, PDO::PARAM_INT);
$query->bindValue(':page_result', $page_result, PDO::PARAM_INT);
$query->execute();
$data = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $key => $value) {
$id = is_int($value['id']);
    var_dump($id);
    echo $id;
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Trippy
  • 81
  • 6

2 Answers2

2

In order for PHP to correctly represent data types from MySQL, you need to have MySQL Native Driver (mysqlnd) installed.

Link: http://php.net/manual/en/book.mysqlnd.php

When you don't have native driver installed, PHP must guess what the type is. The safest type is string.

Function that can determine if a string-representation of something is actually a number is is_numeric.

Alternatively, as you did, you can coerce the type to integer using intval($variable) or (int)$variable.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Mjh
  • 2,904
  • 1
  • 17
  • 16
0

I found a solution :

if ((is_int($value['id']) != true)) {
  $id = intval($value['id']);
}

Now , it's ok

Adam Konieska
  • 2,805
  • 3
  • 14
  • 27
Trippy
  • 81
  • 6