0

I am trying to extract data from mysql database into a datatable using ajax, and php.

The code for my response.php file is below:

<?php
$result = mysql_query("select * from orders");
while ($row = mysql_fetch_array($result)) {
    $data = array(
        array(
            'Name' => $row['jobnumber'],
            'Empid' => $row['ID'],
            'Salary' => $row['product']
        )
    );
}

$results = array(
    "sEcho" => 1,
    "iTotalRecords" => count($data),
    "iTotalDisplayRecords" => count($data),
    "aaData" => $data
);
/*while($row = $result->fetch_array(MYSQLI_ASSOC)){
$results["data"][] = $row ;
}*/

echo json_encode($results);

?>

Why is this only returning one result in my front end table?

http://orca.awaluminium.com/test.php

link above shows table.

2 Answers 2

2

You're replacing value of $data instead of pushing new rows in an array.

Change the following line.

$data = array(
              array(
                    'Name'=>$row['jobnumber'],
                    'Empid'=>$row['ID'], 'Salary'=>$row['product']
              )
);

To

$data[] = array(
             'Name'=>$row['jobnumber'],
             'Empid'=>$row['ID'], 'Salary'=>$row['product'] 
);

Also put $data=array(); before string while() looop.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to do foreach

while ($row = mysql_fetch_array($result)){

foreach($row as $a)
                                    {$data[] = array(
                        array('Name'=>$a['jobnumber'],     'Empid'=>$a['ID'], 'Salary'=>$a['product']),                            
                                                    );
                                    }
}

4 Comments

I have changed it to this, and it seems to be working but not 100%.
it is dragging through the correct amount of rows, it just isn't populating the table with data.
How are you displaying the data? Paste test.php full code in pastebin.com
Sorry for the delayed response, pastebin link is as follows: pastebin.com/4Zw3LJHg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.