Title: How to retrieve wp_ json_encode data from custom WordPress database table
Last modified: February 1, 2018

---

# How to retrieve wp_ json_encode data from custom WordPress database table

 *  Resolved [Mineshrai](https://wordpress.org/support/users/mineshrai/)
 * (@mineshrai)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/)
 * Hi,
    I have created dynamic add/remove fields in frontend post submission page.
   I use `wp_json_encode` and `$wpdb->insert` to insert data into my custom WordPress
   database table. the system is working perfectly.
 * The data is stored in “reward_details” column of my custom table.. It looks like:
 * `{"reward_amount":["500","250","20000","3000"],"reward_title":["Horse","Cat","
   Tiger","Monkey"],"reward_description":["Horse Home","Cat Home","Tiger Home","
   Monkey Home"]}`
 * But however I am not able to retrieve and display the data using `json_decode`.
   Following is my code:
 *     ```
       <?php $project_id = $_SESSION['project_id']; global $wpdb; $string = $wpdb->get_results( "SELECT reward_details FROM wpxa_rewards WHERE project_id = $project_id" );
   
         $someArray = json_decode($string, true);
   
           $count = count( $someArray );
           for ( $i = 0; $i < $count; $i++ ) { ?>
   
       <div class="panel panel-default">
         <div class="panel-body">
            <?php echo $someArray[$i]["reward_title"]; ?>
         </div>
       </div>
   
        <?php } ?>
       ```
   
 * I want to retrieve and display data dynamically… Plz help…

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925248)
 * Laying out youe code to make it more readable:
 *     ```
       <?php
        $project_id = $_SESSION['project_id'];
        global $wpdb;
        $string = $wpdb->get_results(
         "SELECT reward_details FROM wpxa_rewards WHERE project_id = $project_id"
        );
   
         $someArray = json_decode($string, true);
   
           $count = count( $someArray );
           for ( $i = 0; $i < $count; $i++ ) {
        ?>
   
       <div class="panel panel-default">
         <div class="panel-body">
            <?php echo $someArray[$i]["reward_title"]; ?>
         </div>
       </div>
   
        <?php } ?>
       ```
   
 * The problem you describe is now apparent:
    You are only selecting “reward_details”
   but trying to display “reward_title”.
 * A more serious problem is that you are vulnerable to an SQL injection attack.
   
   You must never ever in your whole life EVER pass user input into SQL queries.
   With a browser inspector or other techniques a bad person could send your code
   a value like: `$_SESSION['project_id'] = "1; drop table;` AND it could be much
   worse. The solution is to use “prepare” so that mischievous input is not executed.
   More details here: [https://codex.wordpress.org/Class_Reference/wpdb](https://codex.wordpress.org/Class_Reference/wpdb)
 *  Thread Starter [Mineshrai](https://wordpress.org/support/users/mineshrai/)
 * (@mineshrai)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925349)
 * Hi [@rossmitchell](https://wordpress.org/support/users/rossmitchell/)
 * Thanks for ur answer.
 * I have updated my code as follows it is working fine but there is one big problem,
   it is displaying only first three values…Plz help…
 *     ```
       <?php $project_id = $_SESSION['project_id']; $query = $wpdb->prepare( "SELECT reward_details FROM wpxa_orocox_rewards WHERE project_id = %d", $project_id );
   
       $string = $wpdb->get_var( $query );
   
       $someArray = json_decode( $string, true );
   
          $count = count( $someArray );
           for ( $i = 0; $i < $count; $i++ ) { ?>
   
       <div class="panel panel-default">
         <div class="panel-body">
            <?php echo $someArray['reward_title'][$i]; ?>
         </div>
       </div>
   
       <?php } ?>
       ```
   
 *  anonymized-15380454
 * (@anonymized-15380454)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925467)
 * Try this:
 *     ```
       <?php $project_id = $_SESSION['project_id']; $query = $wpdb->prepare( "SELECT reward_details FROM wpxa_orocox_rewards WHERE project_id = %d", $project_id );
   
       // Use get_col() and not get_var().
       $string = $wpdb->get_col( $query );
   
       // Each $string is a JSON-encoded string saved in the reward_details column.
       foreach ( $string as $s ) {
       	$someArray = json_decode( $s, true );
   
       	   // Counts the number of items in reward_title and not reward_details
       	   // (which is $s in this case).
       	   $count = count( $someArray['reward_title'] );
       		for ( $i = 0; $i < $count; $i++ ) { ?>
   
       	<div class="panel panel-default">
       	  <div class="panel-body">
       		 <?php echo $someArray['reward_title'][$i]; ?>
       	  </div>
       	</div>
   
       	<?php }
       } ?>
       ```
   
    -  This reply was modified 8 years, 4 months ago by anonymized-15380454. Reason:
      Corrected a typo
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925607)
 * As a debugging technique I find that displaying the full details of a structured
   variable is very rewarding. Accordingly I would be using debug code like:
 *     ```
       echo '<div>string is:' . print_r( $string ) . '</div>';
       ```
   
 * It will show you what is in $string it it is a simple string or integer, an object
   or and array and what the elements are in the array.
 *  Thread Starter [Mineshrai](https://wordpress.org/support/users/mineshrai/)
 * (@mineshrai)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925614)
 * Big Thanks @saltennys It works great… 🙂
 *  anonymized-15380454
 * (@anonymized-15380454)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925648)
 * You’re welcome!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘How to retrieve wp_ json_encode data from custom WordPress database table’
is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: anonymized-15380454
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/how-to-retrieve-wp_-json_encode-data-from-custom-wordpress-database-table-2/#post-9925648)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
