php - Unable to extract data from Database and convert it into Json data -
i trying extract data database , convert json data.
i have table id, image , name , price of product. want convert these data json , extract them website.
<?php //config file used connect php db include_once('config.php'); // images table name $sql= "select * `images` "; $res= mysql_query($sql); $result = array(); while ($row = mysql_fetch_array($res)) //image stored longbob, name varchar , price int array_push($result, array('id'=> $row[0], 'image' = > $row[1], 'name'=> $row[2], 'price'=> $row[3] )) echo json_encode(array()); ?>
you don't need array_push. continue array , this:
<?php //config file used connect php db include_once('config.php'); // images table name $sql= "select * `images` "; $res= mysql_query($sql); $result=array(); while (($row = mysql_fetch_array($res))!==false) { //image stored longbob, name varchar , price int $result[] = array('id'=> $row[0], 'image' = > $row[1], 'name'=> $row[2], 'price'=> $row[3], 'error'=>false, 'error_message'=>'' )); } if(count($result)>0) echo json_encode($result); else echo json_encode(array(array('error'=>true,'error_message'=>'no images'))); ?>
i think want in ajax right? if use in ajax, put exit;
on last line of code.
i add error object can debug code or check if data exists or no.
Comments
Post a Comment