# Trouble with using PHP PDO prepared statements

**URL:** <https://community.haxe.org/t/trouble-with-using-php-pdo-prepared-statements/1954>\
**Category:** Haxe\
**Tags:** haxe-php\
**Created:** [August 8, 2019, 9:16pm UTC](https://community.haxe.org/t/trouble-with-using-php-pdo-prepared-statements/1954 "2019-08-08T21:16:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tienery](https://community.haxe.org/user_avatar/community.haxe.org/tienery/32/964_2.png) [@tienery](https://community.haxe.org/u/tienery)\
**Post date:** [August 8, 2019, 9:16pm UTC](https://community.haxe.org/t/trouble-with-using-php-pdo-prepared-statements/1954/1 "2019-08-08T21:16:42Z")

</div>

Hello everyone!

I have been trying to figure out how to operate on fetching executed statements both in Mysqli and PDO without much success. I am currently testing on a very simple example to find out the best approach:

```haxe
var stmt = _dbConn.prepare(query, Lib.toPhpArray([]));
        stmt.execute(Lib.toPhpArray([]));

        var queryResults = stmt.setFetchMode(PDO.FETCH_ASSOC);
        for (k => v in stmt.fetchAll().keyValueIterator())
        {
            Lib.print(k + ": " + v);
        }

```

Which results in:

 ![image](https://community.haxe.org/uploads/default/original/1X/a3159efd13a5f588ccb173790ab9252d6ac37516.png)

`v`, as you can see, is another Key-Value iterator but Haxe doesn’t recognise this, it simply assumes it’s Dynamic.

This is usually fine, except because it’s a key-value iterator I can’t literally create a for loop to iterate over it.

```haxe
var stmt = _dbConn.prepare(query, Lib.toPhpArray([]));
stmt.execute(Lib.toPhpArray([]));

var queryResults = stmt.setFetchMode(PDO.FETCH_ASSOC);
for (k => v in stmt.fetchAll().keyValueIterator())
{
    for (col => value in v)
    {
        Lib.print(col + ": " + value);
    }
}

```

Gives the error: `characters 34-35 : You can't iterate on a Dynamic value, please specify KeyValueIterator or KeyValueIterable`

Attempting to cast the Dynamic type to a `Map<String, String>` also causes the following error: `Cast type parameters must be Dynamic`.

I’ve printed out the result of `Type.typeof(v)` and it’s just `TUnknown`, thinking perhaps there was a specific type I could cast to where Haxe won’t complain. I’m at a dead end now. I’m not sure I want to resort to writing PHP directly, as it defeats the point of what I’m doing.

Any ideas as to how to solve this? Any help greatly appreciated.

---

<div class="post-metadata">

**Author:** ![tienery](https://community.haxe.org/user_avatar/community.haxe.org/tienery/32/964_2.png) [@tienery](https://community.haxe.org/u/tienery)\
**Post date:** [August 8, 2019, 10:48pm UTC](https://community.haxe.org/t/trouble-with-using-php-pdo-prepared-statements/1954/2 "2019-08-08T22:48:29Z")

</div>

> I’ve attempted something else:
> 
> ```haxe
> var rows = cast (v, Map<Dynamic, Dynamic>);
> 
> ```
> 
> This compiled, but I got a runtime error: `Cannot cast [id => 1, name => , description =>] to haxe.IMap `.

Okay, I’ve got there in the end:

```haxe
var stmt = _dbConn.prepare(query, Lib.toPhpArray([]));
stmt.execute(Lib.toPhpArray([]));

var queryResults = stmt.setFetchMode(PDO.FETCH_ASSOC);
for (k => v in stmt.fetchAll().keyValueIterator())
{
    Syntax.foreach(v, (col, value) -> {
        var instance = Type.createInstance(cls, []);
        Reflect.setField(instance, col, value);
        results.push(instance);
    });
}

```

Lots of workarounds required when dealing with dynamic languages it seems.

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [August 9, 2019, 7:31am UTC](https://community.haxe.org/t/trouble-with-using-php-pdo-prepared-statements/1954/3 "2019-08-09T07:31:10Z")

</div>

> [@tienery](#):
>
> Attempting to cast the Dynamic type to a `Map<String, String>` also causes the following error: `Cast type parameters must be Dynamic` .

Try check-type syntax instead of a cast:

```haxe
for(col => value in (v:Map<String,String>)) {
}

```

Keep in mind that unlike cast it does not perform any runtime checks. And if `v` will happen to be e.g. an Array instead of Map at runtime, then the behavior is undefined (most likely an exception about accessing a non-existent method).
