Because the id in the database may be large. If % d is used, the correct result may not be returned because it is out of the range. Therefore, it is recommended that %. 0f be better than % d when formatting the id. Compare to construct SQL statements
The code is as follows:
$ SQL = 'select *
FROM sdb_comments
WHERE goods_id = '. intval ($ goods_id ).'
AND for_comment_id IS NULL
AND object_type = ". $ item ."
AND disabled = "false"
AND display = "true "';
I prefer to do this:
The code is as follows:
$ SQL = sprintf ('select *
FROM sdb_comments
WHERE goods_id = %. 0f
AND for_comment_id IS NULL
AND object_type = "% s"
AND disabled = "false"
AND display = "true" ', (float) $ goods_id, $ item );
This statement is still simple. if it is more complex, concatenating strings is a nightmare.
The second method is more convenient, but a small problem: when formatting a number, you need to pay attention to its value range. Numeric operation. The SQL statement returned at the end is not what we need.
I made a summary today:
% D: 2 ^ 31 ~ 2 ^ 31-1 (-2147483648 ~ 2147483647) (convert int to signed decimal)
% B: Binary (convert int type to binary)
% C: Character (convert int type to character)
% U: 2 ^ 32-1 (0 ~ 4294967295) (convert int to signed decimal)
% F:-2 ^ 128-(-3.4E38 ~ + 3.4E38) (convert float to float) localization
% F:-2 ^ 128-(-3.4E38 ~ + 3.4E38) (convert float to float) non-localization
% O (convert int to octal)
% S: string
% X: converts an int into a hexadecimal string of lowercase letters.
% X: hexadecimal format for converting an int to an uppercase letter
Because the id in the database may be large. If % d is used, the correct result may not be returned because it is out of the range. Therefore, it is recommended that %. 0f be better than % d when formatting the id.