Skip to:
Content
Pages
Categories
Search
Top
Bottom

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

  • @buddypresser

    Participant

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

    Participant

    it’s for php to know how to handle the parameters,

    %d – the argument is treated as an integer, and presented as a (signed) decimal number.

    %s – the argument is treated as and presented as a string.

    in your examples, $slug is a string and $this->id is an integer

    look at http://www.php.net/manual/en/function.sprintf.php for more info

    @buddypresser

    Participant

    Thanks! :D

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘What's the difference between '%s' and '%d' in MySQL statements?’ is closed to new replies.
Skip to toolbar