Reputation: 111
I have this situation with PHP (PDO);
I'm implementing a method that retrieves data from MySql and does it well, but the problem is that duplicate data recovered as key bone as no value but other data appends more, leave the script and images for more detail;
Data.php
//read
public function getData() {
$statement = $this->db->prepare("select Id,Company_Name,Contact_Name,Contact_Title,Address from contact_details LIMIT 3;");
$statement->execute();
if ($statement->rowCount() > 0) {
$value = $statement->fetchAll();
return $value;
}
}
variable inspection
json duplicate
expected result
Data: [{"Id":"1","Company_Name":"Romero y tomillo","Contact_Name":"Alejandra Camino","Contact_Title":"Accounting Manager","Address":null}
,{"Id":"2","Company_Name":"Morgenstern Gesundkost","Contact_Name":"Alexander Feuer","Contact_Title":"Marketing Assistant","Address":"Heerstr. 22"}
,{"Id":"3","Company_Name":"Ana Trujillo Emparedados y helados","Contact_Name":"Ana Trujillo","Contact_Title":"Owner","Address":null}]
Upvotes: 1
Views: 2014
Reputation: 295
change pdo attribute accoding to requirment. . .
$dbConnection = new PDO( 'pgsql:host=' . $host . ';dbname=' . $database, $username,
$password, );
$dbConnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
set / change PDO attribute more detail here
Upvotes: 1
Reputation: 1385
By default PDO uses the PDO::FETCH_BOTH as its fetch result. Meaning that it maps every column to its name and to an 'integer' column name.
Check the fetch_style param= http://php.net/manual/en/pdostatement.fetch.php
For your expected result you will need to use the PDO::FETCH_ASSOC
attribute.
You can or set this as the default FETCH_MODE on the pdo object as follows:
$this->db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
Or you can specify it for every fetch cal:
$value = $statement->fetchAll(PDO::FETCH_ASSOC);
Upvotes: 6