为什么在函数中使用php中的%d

时间:2014-12-19 作者:Ubaid Rehman

$mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) );
有人能告诉我为什么要使用%d和%s吗?在上述代码中?

1 个回复
SO网友:fuxia

$wpdb->prepare() 支持sprintf() arguments. 这个%d 是整数的占位符(不是十进制,应该是%f).

使用%d 如果要向数据库发送实整数,请使用%s 对于字符串。

例如:

$post_id  = \'2\';
$prepared = $wpdb->prepare(
    "SELECT * FROM $wpdb->posts WHERE post_id = %d", 
    $post_id 
);
这里我们使用%d 以确保查询整数。如果我们使用%s, 不会有严格的匹配,因为$post_id 是PHP代码段中的字符串。

$wpdb->prepare() 在不确定值的格式时非常有用。

那么,您如何知道每个字段所需的类型呢?您可以在中查找此信息wp-admin/includes/schema.php. 的示例posts 表格:

CREATE TABLE $wpdb->posts (
  ID bigint(20) unsigned NOT NULL auto_increment,
  post_author bigint(20) unsigned NOT NULL default \'0\',
  post_date datetime NOT NULL default \'0000-00-00 00:00:00\',
  post_date_gmt datetime NOT NULL default \'0000-00-00 00:00:00\',
  post_content longtext NOT NULL,
  post_title text NOT NULL,
  post_excerpt text NOT NULL,
  post_status varchar(20) NOT NULL default \'publish\',
  comment_status varchar(20) NOT NULL default \'open\',
  ping_status varchar(20) NOT NULL default \'open\',
  post_password varchar(20) NOT NULL default \'\',
  post_name varchar(200) NOT NULL default \'\',
  to_ping text NOT NULL,
  pinged text NOT NULL,
  post_modified datetime NOT NULL default \'0000-00-00 00:00:00\',
  post_modified_gmt datetime NOT NULL default \'0000-00-00 00:00:00\',
  post_content_filtered longtext NOT NULL,
  post_parent bigint(20) unsigned NOT NULL default \'0\',
  guid varchar(255) NOT NULL default \'\',
  menu_order int(11) NOT NULL default \'0\',
  post_type varchar(20) NOT NULL default \'post\',
  post_mime_type varchar(100) NOT NULL default \'\',
  comment_count bigint(20) NOT NULL default \'0\',
  PRIMARY KEY  (ID),
  KEY post_name (post_name),
  KEY type_status_date (post_type,post_status,post_date,ID),
  KEY post_parent (post_parent),
  KEY post_author (post_author)
) $charset_collate;\\n";
所有声明为bigintint 需要%d, 因为这些字段是整数。*text, varchardatetime 要求%s.

您还可以在PHP脚本中以编程方式查询该信息:

global $wpdb;

$table_data = $wpdb->get_results( "DESCRIBE $wpdb->posts" );
print \'<pre>\' . print_r( $table_data, 1 ) . \'</pre>\';
结果如下所示:

Array
(
    [0] => stdClass Object
        (
            [Field] => ID
            [Type] => bigint(20) unsigned
            [Null] => NO
            [Key] => PRI
            [Default] => 
            [Extra] => auto_increment
        )

    [1] => stdClass Object
        (
            [Field] => post_author
            [Type] => bigint(20) unsigned
            [Null] => NO
            [Key] => MUL
            [Default] => 0
            [Extra] => 
        )

    [2] => stdClass Object
        (
            [Field] => post_date
            [Type] => datetime
            [Null] => NO
            [Key] => 
            [Default] => 0000-00-00 00:00:00
            [Extra] => 
        )

    [3] => stdClass Object
        (
            [Field] => post_date_gmt
            [Type] => datetime
            [Null] => NO
            [Key] => 
            [Default] => 0000-00-00 00:00:00
            [Extra] => 
        )

    [4] => stdClass Object
        (
            [Field] => post_content
            [Type] => longtext
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [5] => stdClass Object
        (
            [Field] => post_title
            [Type] => text
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [6] => stdClass Object
        (
            [Field] => post_excerpt
            [Type] => text
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [7] => stdClass Object
        (
            [Field] => post_status
            [Type] => varchar(20)
            [Null] => NO
            [Key] => 
            [Default] => publish
            [Extra] => 
        )

    [8] => stdClass Object
        (
            [Field] => comment_status
            [Type] => varchar(20)
            [Null] => NO
            [Key] => 
            [Default] => open
            [Extra] => 
        )

    [9] => stdClass Object
        (
            [Field] => ping_status
            [Type] => varchar(20)
            [Null] => NO
            [Key] => 
            [Default] => open
            [Extra] => 
        )

    [10] => stdClass Object
        (
            [Field] => post_password
            [Type] => varchar(20)
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [11] => stdClass Object
        (
            [Field] => post_name
            [Type] => varchar(200)
            [Null] => NO
            [Key] => MUL
            [Default] => 
            [Extra] => 
        )

    [12] => stdClass Object
        (
            [Field] => to_ping
            [Type] => text
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [13] => stdClass Object
        (
            [Field] => pinged
            [Type] => text
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [14] => stdClass Object
        (
            [Field] => post_modified
            [Type] => datetime
            [Null] => NO
            [Key] => 
            [Default] => 0000-00-00 00:00:00
            [Extra] => 
        )

    [15] => stdClass Object
        (
            [Field] => post_modified_gmt
            [Type] => datetime
            [Null] => NO
            [Key] => 
            [Default] => 0000-00-00 00:00:00
            [Extra] => 
        )

    [16] => stdClass Object
        (
            [Field] => post_content_filtered
            [Type] => longtext
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [17] => stdClass Object
        (
            [Field] => post_parent
            [Type] => bigint(20) unsigned
            [Null] => NO
            [Key] => MUL
            [Default] => 0
            [Extra] => 
        )

    [18] => stdClass Object
        (
            [Field] => guid
            [Type] => varchar(255)
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [19] => stdClass Object
        (
            [Field] => menu_order
            [Type] => int(11)
            [Null] => NO
            [Key] => 
            [Default] => 0
            [Extra] => 
        )

    [20] => stdClass Object
        (
            [Field] => post_type
            [Type] => varchar(20)
            [Null] => NO
            [Key] => MUL
            [Default] => post
            [Extra] => 
        )

    [21] => stdClass Object
        (
            [Field] => post_mime_type
            [Type] => varchar(100)
            [Null] => NO
            [Key] => 
            [Default] => 
            [Extra] => 
        )

    [22] => stdClass Object
        (
            [Field] => comment_count
            [Type] => bigint(20)
            [Null] => NO
            [Key] => 
            [Default] => 0
            [Extra] => 
        )
)
您现在可以使用Type 输入以确定正确的占位符。对于核心表,我不会这样做,只需使用在schema.php. 来自第三方代码的自定义表是另一种情况,检查这些表有时可能有用。

结束

相关推荐

与PHP升级相关的WordPress错误

我主动提出在我的网络托管帐户上托管我堂兄的演员简介网站。他让人创建了一个wordpress网站并将其存储在我的帐户上。它在PhP 5.2上运行得很好,但我的主机提供商已经将其淘汰。现在,我被迫使用PhP 5.3或PhP 5.4,因此出现了许多错误。如果有人知道是否/如何解决这个问题,请告诉我好吗?他依靠这个网站来获得工作,我很沮丧,因为我找不到答案,也无法自己解决。使用PhP 5.4,我在页面内容上方发现以下错误:Strict Standards: Declaration of Walker_Page::