sprintf在我的查询中返回0

Can you use sprintf like this:

$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');

I get a MySQL syntax error in this part of the query:

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

Regards

In short, No. you cannot use sprintf like that, you should take a look at the docs for sprintf:

http://php.net/sprintf

%a doesn't represent a type specifier, which is why it outputs in your query.

%d treats arguments as integers, and you are passing strings.

Using sprintf properly, your code should look like this:

$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');