Title: Access database by SQL quiries
Last modified: November 10, 2017

---

# Access database by SQL quiries

 *  Resolved [dacgarz](https://wordpress.org/support/users/dacgarz/)
 * (@dacgarz)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/access-database-by-sql-quiries/)
 * Hi,
 * I was wondering if there is a way to retrieve the timestamps on the latest entry
   by receiver email and subject line. I’m trying show it on the front end where
   the timestamp would show as the button text after the email is sent. Make sense?
 * Basically I’ve using the usually way of retrieving specific variables by SQL 
   SELECT function and it didn’t work on this.
    $lastemailed = $wpdb->get_var($wpdb-
   >prepare(“SELECT timestamp FROM $wpdb->wpml_mails WHERE receiver=%s AND subject
   =%s”, $useremail, $list_subj));
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Faccess-database-by-sql-quiries%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [No3x](https://wordpress.org/support/users/no3x/)
 * (@no3x)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/access-database-by-sql-quiries/#post-9671634)
 * Hi [@dacgarz](https://wordpress.org/support/users/dacgarz/),
 * Of course you can query whatever you want on the database table. There is no 
   build in support in my ORM for what you are trying to do so you have to use plain
   SQL.
    There are issues with your query: `$lastemailed = $wpdb->get_var($wpdb-
   >prepare("SELECT timestamp FROM $wpdb->wpml_mails WHERE receiver=%s AND subject
   =%s", $useremail, $list_subj));` Please enable WP_DEBUG and try to read error
   messages.
 * 1. `$wpdb->wpml_mails`
    `$wpdb` has no member wpml_mails. Use `$tablename = $
   wpdb->prefix . 'wpml_mails';` instead. 2. Order by date to get latest 3. Quote
   your search strings 4. Limit is not required by the nature of get_var.
 *     ```
       global $wpdb;
       $tablename = $wpdb->prefix . 'wpml_mails';
       $timestamp = $wpdb->get_var($wpdb->prepare("SELECT timestamp FROM $tablename WHERE receiver=\"%s\" AND subject=\"%s\" ORDER BY timestamp DESC", $useremail, $list_subj));
       submit_button( $text = $timestamp, $type = 'secondary', $name = 'button_latest' );
       ```
   
 * Please note that the receiver and subject must match exactly with your current
   query. You might want to use LIKE.
    -  This reply was modified 8 years, 7 months ago by [No3x](https://wordpress.org/support/users/no3x/).
      Reason: typo
 *  Thread Starter [dacgarz](https://wordpress.org/support/users/dacgarz/)
 * (@dacgarz)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/access-database-by-sql-quiries/#post-9671764)
 * Hi No3x,
 * Thank you so much for the help. It is working like how I wanted it to. I just
   had to remove the submit_button() part and made it fit to my current form setup.
 * Thanks again.

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

The topic ‘Access database by SQL quiries’ is closed to new replies.

 * ![](https://ps.w.org/wp-mail-logging/assets/icon-256x256.jpg?rev=2562296)
 * [WP Mail Logging](https://wordpress.org/plugins/wp-mail-logging/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-mail-logging/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-mail-logging/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-mail-logging/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-mail-logging/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-mail-logging/reviews/)

## Tags

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

 * 2 replies
 * 2 participants
 * Last reply from: [dacgarz](https://wordpress.org/support/users/dacgarz/)
 * Last activity: [8 years, 7 months ago](https://wordpress.org/support/topic/access-database-by-sql-quiries/#post-9671764)
 * Status: resolved