Skip to:
Content
Pages
Categories
Search
Top
Bottom

What's the difference between '%s' and '%d' in MySQL statements?

  • Avatar of buddypresser
    BuddyPresser
    Participant

    @buddypresser

    I’m just confused by something and can’t find anything on Google (since it doesn’t like searching for characters..)

    What’s the difference between ‘%s’ and ‘%d’, when used in MySQL statements passed to $wpdb->prepare?

    It’s pretty clear what the code actually does, but when writing my own DB access functions, I’m confused as to which to use.

    For example,

    $wpdb->prepare( "DELETE FROM {$bp->groups->table_name} WHERE id = %d", $this->id )

    uses ‘%d’, but

    $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", $slug

    uses ‘%s’ …

    Then, in this, both are used:

    $sql = $wpdb->prepare(<br />
    "UPDATE {$bp->groups->table_name} SET<br />
    creator_id = %d,<br />
    name = %s,<br />
    slug = %s,<br />
    description = %s,<br />
    news = %s,<br />
    status = %s,<br />
    enable_wire = %d,<br />
    enable_forum = %d,<br />
    date_created = FROM_UNIXTIME(%d)<br />
    WHERE<br />
    id = %d<br />
    ",<br />
    $this->creator_id,<br />
    $this->name,<br />
    $this->slug,<br />
    $this->description,<br />
    $this->news,<br />
    $this->status,<br />
    $this->enable_wire,<br />
    $this->enable_forum,<br />
    $this->date_created,<br />
    $this->id<br />
    );

    .. and I can’t work out the difference!

    It’s probably really simple, too.

    Thanks in advance!

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

You must be logged in to reply to this topic.