0

Trying to get array from database rows with numeric indices only with:

$sql = "SELECT * from resumenes";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $stmt->fetch(PDO::FETCH_NUM);
    foreach ($stmt as $row) {
        print_r($row);
    }

I'm getting both, numeric indices and associative indices as if he had used PDO::FETCH_BOTH

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Please check the code in your question. That's not how [fetch()](https://www.php.net/manual/en/pdostatement.fetch.php) should be used. Always put the exact code in your question that can reproduce the problem. – KIKO Software Oct 20 '21 at 08:50

1 Answers1

0

3 possibilities :

  1. Use $row returned by $stmt->fetch(PDO::FETCH_NUM) in a loot
$stmt = $db->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    print_r($row);
}
  1. Specify request fetch mode :
$stmt = $db->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_NUM); // Fetch mode for this statement
$stmt->execute();
foreach ($stmt as $row) {
    print_r($row);
}
  1. Specify default fetch mode, tipically after connection :
$pdo = new PDO(...);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM);
JCH77
  • 1,125
  • 13
  • 13