json的结果没有显示,并且在php中没有出现错误

时间:2016-05-09 14:37:33

标签: php json

我在php中有一个代码,在json中给出结果,当我在浏览器上尝试php脚本时,没有结果这里是php文件和我的mysql表

<?php 
    //Importing Database Script 
    require_once('dbConnect.php');

    //Creating sql query
    $sql = "SELECT * FROM EnfantVaccin";

    //getting result 
    $r = mysqli_query($con,$sql);

    //creating a blank array 
    $result = array();

    //looping through all the records fetched
    while($row = mysqli_fetch_array($r)){

        //Pushing name and id in the blank array created 
        array_push($result,array(
            "enfant_vacc_id"=>$row['enfant_vacc_id'],
            "enfant_id"=>$row['enfant_id'],
            "nomVaccin"=>$row['nomVaccin'],
            "ageApproprie"=>$row['ageApproprie'],
            "etat"=>$row['etat'],
            "date_vaccin"=>$row['date_vaccin']
        ));
    }

    //Displaying the array in json format w samineh result itableau fi json format 5ater valeur mta3 notre TAG_JSON_ARRAY hia result
    echo json_encode(array('resultVaccinEnf'=>$result));

    mysqli_close($con);

这就是mysql表'EnfantVaccin'

CREATE TABLE `EnfantVaccin`(
`enfant_vacc_id`  bigint(50) unsigned  NULL DEFAULT NULL AUTO_INCREMENT,
 `enfant_id` bigint(50) unsigned  NULL DEFAULT NULL ,
 `nomVaccin` varchar(20)  NOT NULL,
`ageApproprie` int(10) NOT NULL,
`etat` boolean NOt null ,/*ca indique si ce vaccin particulier est fait pour cet enfant particulier ou pas */
`date_vaccin` date not null,/*indique la date de ce vaccin pour cet enfant*/
PRIMARY KEY (`enfant_vacc_id`) ,
CONSTRAINT `FK_enf2` FOREIGN KEY (`enfant_id`) REFERENCES `enfant` (`enfant_id`)
 )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

这就是我在表格中的行

enter image description here

我希望我能找到帮助,我真的很困惑

1 个答案:

答案 0 :(得分:0)

 if($stmt = mysqli_prepare($con, $sql)){
        //execute query
        mysqli_stmt_execute($stmt);
        //bind results
        mysqli_stmt_bind_result($stmt, $enfant_vacc_id, $enfant_id, $nomVaccin, $ageApproprie, $etat, 
        $date_vaccin); 
        //store result so num rows can be counted
        $r = mysqli_stmt_store_result($stmt);
        $result = array();
        //fetch results 
        while (mysqli_fetch_array($r)) {
        //Pushing name and id in the blank array created 
        array_push($result,array(
            "enfant_vacc_id"=>$enfant_vacc_id,
            "enfant_id"=>$enfant_id,
            "nomVaccin"=>$nomVaccin,
            "ageApproprie"=>$ageApproprie,
            "etat"=>$etat,
            "date_vaccin"=>$date_vaccin
        ));
        }
        //Displaying the array in json format w samineh result itableau fi json format 5ater valeur mta3 notre TAG_JSON_ARRAY hia result
    echo json_encode(array('resultVaccinEnf'=>$result));
    mysqli_close($con); 
    }
    else {
    // Error 
    printf("Prepared Statement Error: %s\n", $db->error);
    }