1
<?php 

$link = mysql_connect('localhost', 'root', 'admin')
or die('There was a problem connecting to the database.');
mysql_select_db('hospitalmaster');

$hnum = (int)$_POST["hnum"];
$sql = "SELECT d.doctorid, d.doctorname 
        from hospitalmaster.doctor_master d 
            inner join pharmacymaster.pharbill e 
        where e.hnum = '$hnum' 
        and e.presid = d.d_empno 
        group by e.presid";

$result = mysql_query($sql);
$response = array();

if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $response = $row;
    }

    echo json_encode(array("Doctor"=>$response));
} else {
    echo ("no DATA");
}
?>

i have the api shown above, but this api is returning me as json objects not an json array? i would like to know how to get dotorid an doctorname as a array, since i have many doctor names and id, i want each doctor and their corresponding id as an idividual array, right now they are returning as individual objects. Since this is my first time writing an api, i dont know how to modify the code.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jul 16 '17 at 16:53
  • Adding to above comment you can convert json object to array through `json_decode($yourjsonobjectvariable,true);` – Alive to die - Anant Jul 16 '17 at 16:55
  • thank you for the valuable information, i am doing it for a project, therefore time of the essence....so with a few references i learnt this manner...therefore i need know how to modify the code... – nithinsampath Jul 16 '17 at 16:56
  • @AlivetoDie....so my object variable will be docotrs right and the code should be placed below the echo code right? – nithinsampath Jul 16 '17 at 16:58

1 Answers1

3

You are over writing the array each time round the loop.

while ($row = mysql_fetch_assoc($result)) {
    $response[] = $row;
    //change ^^
}

You should use either mysqli_ or PDO. Here is a suggestion for mysqli_

<?php 

$link = mysqli_connect('localhost', 'root', 'admin','hospitalmaster')
or die('There was a problem connecting to the database.');

$sql = "SELECT d.doctorid, d.doctorname 
        from hospitalmaster.doctor_master d 
            inner join pharmacymaster.pharbill e 
        where e.hnum = ?
        and e.presid = d.d_empno 
        group by e.presid";

$stmt = $link->prepare($sql);
$stmt->bind_param('i', (int)$_POST["hnum"]);
$stmt->execute();

if ($stmt->num_rows() > 0) {
    $result = $stmt->get_result();
    $response = array();

    while ($row = $result->fetch_assoc()) {
        $response[] = $row;
    }

    echo json_encode(array("Doctor"=>$response));
} else {
    echo ("no DATA");
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149