Title: SQL Statement in the Editor or Template
Last modified: November 15, 2016

---

# SQL Statement in the Editor or Template

 *  [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/)
 * I created a table within my wordpress database for special use and is trying 
   to select data from that table. I tried inserting an SQL statement in the “Page”
   editor but it didn’t work. I then created a template and insert the SQL statement
   there as well with no luck. How do I go about inserting in a custom SQL statement
   so that I can use the data and display it to the front end?

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437160)
 * Depending on where the data needs to go, I’d suggest either do it as a custom
   template (which you’ve tried) or [set up a shortcode](https://codex.wordpress.org/Shortcode_API)
   for it.
 * I’d probably start off with a template if it’s only going to be used in one place.
   That way you’re not using that code anywhere else in the system where it’s not
   needed.
 * Also, for database queries, it’s mostly a good idea to use the `$wpdb` object.
   It has a lot of good built-in functions that will help out.
 * You did say that the template didn’t work, but how did it not work? If you can
   set up a [pastebin](http://pastebin.com/) with the code we could have a look 
   through it.
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437181)
 * I’m using the typical SELECT statement.
 *     ```
       $servername = "localhost";
       $username = "xxx";
       $password = "xxx";
   
       // Create connection
       $conn = new mysqli($servername, $username, $password);
   
       // Check connection
        if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
       } 
   
       $sql = mysqli_query($conn,"SELECT * FROM dsp_user_settings");
       echo $sql['userId'];
       ```
   
    -  This reply was modified 9 years, 7 months ago by [paulsvang](https://wordpress.org/support/users/paulsvang/).
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437188)
 * I have a table with the column userId, globalShipping, freeShipping and I want
   to be able to pull them in. THe table is named dsp_user_settings.
 * If I can do it with $wpdb, I dont mind trying that route. I appreciate it Catacaustic!
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437195)
 * The `$wpdb` way is easier, and doesn’t need you to set up any extra databaseo
   connections.
 * As a very broad example…
 *     ```
       // You may need the next line, depending on the way your template is set up. You may be able to leave it out
       global $wpdb;
   
       $query = "SELECT userId, globalShipping, freeShipping FROM dsp_user_settings";
       $result = $wpdb->get_results ($query);
   
       foreach( $result as $row ) {
           echo '<p>ID: '" . $row->userId . "'</p>';
       }
       ```
   
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437208)
 * I will give that a try.
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437222)
 * I modified it and got it to work. How would I go about just looking for one record
   so that I avoid the foreach loop.
 *     ```
        $query = "SELECT userId FROM dsp_user_settings WHERE userID = 1";
       $result = $wpdb->get_results ($query);
       echo  $result->userId ;
       ```
   
 * I tried what I modified and it didn’t work. Am I doing something wrong here?
    -  This reply was modified 9 years, 7 months ago by [paulsvang](https://wordpress.org/support/users/paulsvang/).
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437231)
 * You need to read up on the [codex page for WPDB](https://codex.wordpress.org/Class_Reference/wpdb).
   That will show you how things work.
 * You are almost there, but for a single record you need to use a different function.
 * If you want a whole row, you can use this:
 *     ```
       $query = "SELECT userId FROM dsp_user_settings WHERE userID = 1";
       $row = $wpdb->get_row ($query);
       echo  $result->userId;
       ```
   
 * If you want just a single value only, you can use this:
 *     ```
       $query = "SELECT userId FROM dsp_user_settings WHERE userID = 1 LIMIT 1";
       $value = $wpdb->get_var ($query);
       echo  $value;
       ```
   
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437237)
 * I will definitely read up on it. And thanks for your input again! Good night.
 *  Thread Starter [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * (@paulsvang)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437242)
 * Your code worked by the way. Thanks a bunch!

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

The topic ‘SQL Statement in the Editor or Template’ is closed to new replies.

## Tags

 * [page](https://wordpress.org/support/topic-tag/page/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [paulsvang](https://wordpress.org/support/users/paulsvang/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-in-the-editor-or-template/#post-8437242)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
