ryantkelly ryantkelly - 20 days ago 8
PHP Question

PHP PDO execute() returning duplicate results

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!

Answer

Solved by checking with someone better at databases than myself...

I foolishly named the columns as integer values.

So, what appeared as "duplicate values" was just the fact that the column number and the column name were the same!

In conclusion, I am bad at database structure.

Comments