Title: $wpdb function.
Last modified: August 20, 2016

---

# $wpdb function.

 *  [yadigit](https://wordpress.org/support/users/yadigit/)
 * (@yadigit)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/wpdb-function/)
 * How do I get fetch all the data inside of a table?..
    example table Promo.. I
   need to search the table to see if “joe1234” is under the column of “id”.. if
   joe1234 is NOT in Promo => ID I need to insert the user name in the table.. if
   not echo “already taken”… I’ve gotten pretty much everything set up except for
   the fact of calling the data from the database.. Here’s the code ive been using`
   $data = mysql_query(“SELECT * FROM promocode”) or die(mysql_error(‘not connected’));
   $info = mysql_fetch_array( $data );
 *  if ($info[‘ID’] == $current_user->user_login) {
    $wpdb->insert( ‘promocode’ ,
   array( ‘CODE’ => $promocode , ‘ID’ => $myname ), array( ‘%s’, ‘%s’ ) ); }

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

 *  [michael.mariart](https://wordpress.org/support/users/michaelmariart/)
 * (@michaelmariart)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/wpdb-function/#post-2920004)
 * There’s no reason at all to try to return ALL of the data from that table. That
   could be 1,000’s or 1,000,000’s or rows dpeending on just how big it gets so 
   that’s a huge overhead that’s just not needed. You jsut need to know if one songle
   row matches your ID.
 * You can set up your SQL query to be:
 *     ```
       $result = $wpdb->get_row ("SELECT ID FROM Promo WHERE ID = 'joe1234' LIMIT 1");
   
       if (is_null ($row)) {
           // Insert...
       }
       ```
   
 * That way $row will either be a NULL value or it will contain an object with the
   ID. If it’s NULL you know that there’s no user with that ID, so you can then 
   go and add in the user.
 *  Thread Starter [yadigit](https://wordpress.org/support/users/yadigit/)
 * (@yadigit)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/wpdb-function/#post-2920076)
 * Thank you Michael.
    So, in other words, I would take the code that you listed..
   and let’s say I take joe1234 am i able to make it a variable? Example ($logged_in_user)?.
 * so the code would look like this.,
 *     ```
       $logged_in_user = $current_user->logged_in;
       $result = $wpdb->get_row ("SELECT ID FROM Promo WHERE ID = '$logged_in_user' LIMIT 1");
   
       if (is_null ($row)) {
           $wpdb->insert( 'promocode' , array( 'CODE' => $promocode , 'ID' => $logged_in_user ), array( '%s', '%s' ) );
   
       }
       ```
   
 * Would that be the correct code?

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

The topic ‘$wpdb function.’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [yadigit](https://wordpress.org/support/users/yadigit/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/wpdb-function/#post-2920076)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
