Vijay Kumar Vijay Kumar - 7 months ago 47
SQL Question

PHP get duplicate values while fetching data from mysql

PHP get duplicate values in array while fetching data from mysql. i have tried array_unique() but is not working what should i do i am still finding way.

Array
(
[0] => Array
(
[actcode] => Auck1
[0] => Auck1
[country] => New Zealand
[1] => New Zealand
)

)


i am fetching data as this way

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT `actcode`, `country` FROM `activity` WHERE `actcode` = :actcode');
$stmt->execute(array('actcode' => $actcode));
$count=$stmt->rowCount();
$activity_data_array = array();
while($row = $stmt->fetch()) {
$activity_data_array[] = $row;
}
print_r($activity_data_array);

Answer

@MarkBaker is right, he has given you a correct solution just use this fatch mode like

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); may be you dont need this code.
$stmt = $conn->prepare('SELECT `actcode`, `country` FROM `activity`  WHERE `actcode` = :actcode');
$stmt->execute(array('actcode' => $actcode));
$count=$stmt->rowCount();
$activity_data_array = array();
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) { // here just simply use fetchAll(PDO::FETCH_ASSOC) and you will get what you want.
    $activity_data_array[] = $row;
    }
print_r($activity_data_array);

or

you can use it as default

$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
while($row = $stmt->fetch()) // leave this code same.

May be it will help you more.