Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO execute() returning duplicate results [duplicate]

Tags:

php

mysql

pdo

For this code:

$title = '0';
$subTitle = '1';
$tableName = 'someString';

$stmt = $dbh->prepare("SELECT `".$title."`, `".$subTitle."` FROM `".$tableName."_data` WHERE `id` = :key");

$stmt->bindValue(':key', '127');
$stmt->execute();
$result = $stmt->fetchAll();
print_r($result);

I get $result as this:

Array
(
    [0] => Array
        (
            [0] => 91651
            [1] => 91651 - DESCRIPTION
            [2] => 91651 - DESCRIPTION
        )

)

When the expected result should be this:

Array
(
    [0] => Array
        (
            [0] => 91651
            [1] => 91651 - DESCRIPTION
        )

)

When I run the same query in mySQL, it returns the expected result. When it is executed via PHP PDO, it adds a duplicate.

Thanks!

like image 357
ryantkelly Avatar asked Feb 24 '26 02:02

ryantkelly


1 Answers

Use fetchAll(PDO::FETCH_ASSOC) instead of fetchAll().

like image 160
synkro Avatar answered Feb 26 '26 17:02

synkro