sprintf在我的查询中返回0

时间:2015-07-09 15:01:53

标签: php mysql

你可以像这样使用sprintf:

$query = '
        SELECT a.id, a.name, a.sort_code, a.account_number, a.swift_code, %a
        FROM hr_employees_bank a
        LEFT JOIN hr_employees b ON b.id = a.employee
        %d
        WHERE a.employee = :id
        AND b.admin = :admin_id 
        ';

$query = sprintf($query, 'c.name as account_status', 'LEFT JOIN crm_bank_account_statuses c ON a.account_status = c.id');

我在查询的这一部分出现了MySQL语法错误:

%a FROM hr_employees_bank a LEFT JOIN hr_employees b ON b.id = a.employee

此致

1 个答案:

答案 0 :(得分:1)

简而言之,不,你不能像这样使用sprintf,你应该看一下sprintf的文档:

http://php.net/sprintf

%a不代表类型说明符,这就是它在您的查询中输出的原因。

%d将参数视为整数,并且您正在传递字符串。

正确使用sprintf,您的代码应如下所示:

$query = '
    SELECT a.id, a.name, a.sort_code, a.account_number, a.swift_code, %s
    FROM hr_employees_bank a
    LEFT JOIN hr_employees b ON b.id = a.employee
    %s
    WHERE a.employee = :id
    AND b.admin = :admin_id 
    ';

$query = sprintf($query, 'c.name as account_status', 'LEFT JOIN crm_bank_account_statuses c ON a.account_status = c.id');