Title: PHP Variable echo and WP-JSON
Last modified: November 18, 2018

---

# PHP Variable echo and WP-JSON

 *  [Ibby](https://wordpress.org/support/users/ibby/)
 * (@ibby)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/)
 * I am building a mobile application and I am trying to use WP REST API for the
   first time. My apologies if this has been answered many times before but my search
   brought no results unfortunately.
 * I have a custom theme page template which has a SQL database call for some information
   and stores this in an array. It then processes the array and stores all of the
   array data into independent variables which are then echoed onto the page. This
   works fine on the website, but when I attempt to grab the variable outputs using
   the JSON URL, none of the echoed data is shown. I am aware that associative arrays
   and custom objects cannot be held by the REST API and this is why I stored the
   outputs into local variables.
 * Can someone explain why this is not working please?
 *     ```
       //Return the details for this month
       $db=dbConnect(); // Connect to database
       $thisday = date("Ymd");
   
       $query="select GregDate, aa, bb, cc from aaTimes where GregDate=$thisday";
   
       $result = mysql_query($query);
       $num_rows = mysql_num_rows($result);
       $aaSTR="";
   
       while($rs = mysql_fetch_array($result)) {
       	$GregDate=$rs[0];
       	$Aa=$rs[1];
       	$Bb=$rs[2];
       	$Cc=$rs[3];
       }
   
       echo mysql_error();
       dbClose($db);
   
       echo "<p>$Aa</p>";
       echo "<p>$Bb</p>";
       echo "<p>$Cc</p>";
       ```
   
 * Thanks in advance.

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

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10892762)
 * This code is the PHP in the theme tempplate file. There is nothing related to
   the REST API or JSON.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10892821)
 * If the above code is built into your theme, then I don’t think the REST API has
   the ability to execute it.
 * Is there a specific reason why you’re outputting the error status of the query?
   That doesn’t seem necessary. More concerning, however, is that you’re using `
   mysql` functions as opposed to `mysqli` functions.
 * Also, if the database that contains the `aaTimes` table can be accessed by the
   DB user in your WordPress installation, you should use the database access built
   in to WordPress. Opening another DB connection should be avoided if at all possible.
 *  Thread Starter [Ibby](https://wordpress.org/support/users/ibby/)
 * (@ibby)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10892838)
 * [@joyously](https://wordpress.org/support/users/joyously/), sorry my mistake 
   for not explaining fully, that code is placed in the page body area to output
   the variables onto the actual page. It is those HTML outputs I want to make available
   via the REST API.
 * [@diondesigns](https://wordpress.org/support/users/diondesigns/), I am converting
   all of my code to mysqli, so this will be fixed. Would the REST API not be able
   to output the echos in this case? The error status was just for debug and I should
   have deleted it.
    Also what you said at the end there has peaked my attention;
   how do I use the built-in db access available to WP? I am not very experienced
   with WP-PHP though I knnow my way around. I usually just define a function for
   the db connection and then call the connection when required. It sounds like 
   I’m doing it wrong from what you said.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10893238)
 * I don’t think you understand how the REST API works. Your code would need to 
   be in a function, the endpoint you define must call that function, and the output
   should be JSON, not HTML. It honestly sounds like what you want is an AJAX handler.
 * If your custom table is accessible by the WordPress DB user, then you can use
   the following code:
 *     ```
       global $wpdb;
       $dbh = $wpdb->__get('dbh');
       ```
   
 * The `$dbh` variable will contain the connection handle (called _link identifier_
   in the PHP documentation), which is needed for most mysqli procedural functions.
   If you use this code, make absolutely certain you do not close the DB connection,
   or bad things will happen. 🙂
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10893251)
 * You can use the global $wpdb object which contains the default WP DB connection
   to communicate with and run queries on the DB server.
 * What you do on a template file is completely irrelevant to a mobile app (unless
   your app has a built-in HTML interpreter :)). PHP is not even the correct programming
   language. The REST API can only handle standard WP objects by default. It will
   not give you custom data unless you add your own REST route and endpoints. Which
   would be in such a case written in PHP via a plugin.
 * Alternately, manage your data through WP objects like a post object’s meta data.
   Be aware that meta data queries are very inefficient. For optimal performance,
   custom route and endpoints (which run queries through global $wpdb) will be preferable.
 *  Thread Starter [Ibby](https://wordpress.org/support/users/ibby/)
 * (@ibby)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10899352)
 * Honestly, I actually had no idea WP had an in-built class for database connections.
   Thank you very much to everyone.
 * I will try to do it this way and create a function which calls the variables 
   Aa, Bb and Cc and see if I get an output in the JSON.

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

The topic ‘PHP Variable echo and WP-JSON’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 4 participants
 * Last reply from: [Ibby](https://wordpress.org/support/users/ibby/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/php-variable-echo-and-wp-json/#post-10899352)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
