Why are there duplicate rows in my table?

Why are there duplicate rows in my table?

profoundhypnoticprofoundhypnotic Posts: 12Questions: 5Answers: 0

Hello, I was trying to do the tutorial example here: https://datatables.net/examples/ajax/objects.html

I had to make some adjustments to the initialization call but I eventually got data to populate my table. However, there are duplicates of every row in the table. It looks like the call is run twice though in the network tab I only see one ajax call. I have verified my data and there are no duplicate rows when I run the query directly against the database and when I run the php file by itself.

HTML/JS:

<table id="training" class="display cell-border" style="width:98%;table-layout:fixed;">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Function</th>
                        <th>Date</th>
                        <th>Score</th>
                    </tr>
                </thead>
</table>
    <script>
        $(document).ready(function () {
            $('#training').DataTable({
                ajax: {
                    url: 'test.php',
                    dataSrc: ''
                },
                columns: [
                    { data: 'Name'},
                    { data: 'func'},
                    { data: 'date'},
                    { data: 'score'}
                ]
            });
        });
    </script>

PHP:

    try {
        $conn = new PDO ($o,$user,$p);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOExeception $e){
        echo "Connect Failed: " . $e->getMessage();
    }

    $qry = "select Employee.Name,Scores.func,Scores.date,Scores.score
    from Scores
    right join Employee on Scores.emp_id = Employee.ID
    where Employee.Workgroup = 'Gen_Assy'
    having Scores.score > 0";
    $stmt = $conn->prepare($qry);
    $stmt->execute();
    $result = $stmt->fetchall(PDO::FETCH_ASSOC);
    echo json_encode($result);

Answers

Sign In or Register to comment.