2

I am fetching data from database and output the data in json but getting empty JSON. Don't know why? I am getting an empty array posts. you can check my code, can you please help me out.

Here is my code

<?php
//Turn off all error reporting
//error_reporting(0);
ini_set ("display_errors", "1");

  error_reporting(E_ALL);

  define("ENCRYPTION_KEY", "!@#$%^&*");



  /**
  * Returns an encrypted & utf8-encoded
  */
   function encrypt($pure_string, $encryption_key) {
   $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
  return $encrypted_string;
   }

  /**
  * Returns decrypted original string
   */
  function decrypt($encrypted_string, $encryption_key) {
   $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
   return $decrypted_string;
   }


  if(($_GET['action'])&&($_GET['username'])&&($_GET['key'])) {

     $select = $_GET['action'];
     $username =$_GET['username']; //no default
      $key= $_GET['key'];
      if($key=='India'){
      if($select=='select'){
        /* connect to the db */
     $connect = mysqli_connect('localhost','root','')or die("Couldn't connect to database!");
     mysqli_select_db($connect,'easy_sign') or die ("Couldn't find database");



 $query ="SELECT * FROM origin WHERE username ='$username' ";
 $result = mysqli_query($connect,$query);
 $numrows=mysqli_num_rows($result);
 if($numrows!==0)
 {

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

  $username = $row['username'];
  $path =     $row['path'];
  $decrypted_path = decrypt($path, ENCRYPTION_KEY);
  $filename = $row['filename'];
  $decrypted_name = decrypt($filename, ENCRYPTION_KEY);
  $date = $row['date'];

   }




  /* create one master array of the records */

  $posts = array();
  if(mysqli_num_rows($result)) {
    while($post = mysqli_fetch_assoc($result)) {
        $posts[] = array('post'=>$post);
    }
   }

    /* output in necessary format */

    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));




     /* disconnect from the db */
    @mysqli_close($link);
      }
    }
   }

   }
   ?>
7
  • try to remove header('Content-type: application/json'); Commented Nov 4, 2016 at 5:51
  • no effect buddy.. Commented Nov 4, 2016 at 5:57
  • are you getting any data in while($post = mysqli_fetch_assoc($result)). where is query ? Commented Nov 4, 2016 at 6:05
  • $result = mysqli_query($connect,$query); here it declared Commented Nov 4, 2016 at 6:06
  • why you write while loop second time? you can use all json related code first while loop Commented Nov 4, 2016 at 6:11

2 Answers 2

2

Please change if($numrows!==0) to if($numrows > 0) And you can ready your json same as me like bellow :

<?php
$query ="SELECT * FROM origin WHERE username ='$username' ";
$result = mysqli_query($connect,$query);
$numrows = mysqli_num_rows($result);

if($numrows > 0)
{
    $post = array();
    while($row = mysqli_fetch_array($result))
    {
        $path =     $row['path'];
        $filename = $row['filename'];

        $post['username'] = $row['username'];
        $post['decrypted_path'] = decrypt($path, ENCRYPTION_KEY);
        $post['decrypted_name'] = decrypt($filename, ENCRYPTION_KEY);
        $post['date'] = $row['date'];
    }
    echo json_encode($post);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

try this

if($numrows!==0)
{

  $posts = array();
  $data=array();
  while($row = mysqli_fetch_array($result))
   {

     $posts[]=array('username'=>$row['username'],'path'=>$row['path'],'decrypted_path '=>decrypt($path, ENCRYPTION_KEY),'filename'=>$row['filename'],'decrypted_name'=>decrypt($filename, ENCRYPTION_KEY),'date'=>$row['date']);

  }

    $data['posts']=$posts;

    /* output in necessary format */

    header('Content-type: application/json');
    echo json_encode($data);


     /* disconnect from the db */
    @mysqli_close($link);
}

Comments

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.