“%sUser”和“%d”在此代码中的含义是什么?

时间:2010-08-29 21:11:27

标签: php string-formatting

PHP函数:

public static function getById($uid)
{
    $u = new User();

    $query = sprintf('SELECT USERNAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE ' .
        'FROM %sUSER WHERE USER_ID = %d',
        DB_TBL_PREFIX,
        $uid);
    $result = mysql_query($query, $GLOBALS['DB']);

    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_assoc($result);
        $u->username = $row['USERNAME'];
        $u->password = $row['PASSWORD'];
        $u->emailAddr = $row['EMAIL_ADDR'];
        $u->isActive = $row['IS_ACTIVE'];
        $u->uid = $uid;
    }
    mysql_free_result($result);

    return $u;
}

请帮助我,我无法理解这个“%sUser”和“%d”的意思是什么

2 个答案:

答案 0 :(得分:3)

%s是一个“字符串”,正在替换为DB_TBL_PREFIX

的值

%d是一个数字,正在被$uid

的值取代

http://us3.php.net/manual/en/function.sprintf.php

答案 1 :(得分:0)

%s%dsprintf的字符串参数的类型说明符。在这里查看函数的文档:http://us2.php.net/sprintf