• 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)
  • 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

    (@yadigit)

    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.