FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Will this work?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Will this work? [message #172266 is a reply to message #172265] Tue, 08 February 2011 17:23 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On 08/02/11 16:36, Derek Turner wrote:
> On Tue, 08 Feb 2011 15:35:05 +0000, Denis McMahon wrote:
>
>
>> <?php
>>
>> $fmt = "SELECT `workName`, `composer`, SUBSTRING(`Reference`,
>> LOCATE(\'%02d-\', `Reference`)) as TRACK FROM `work` WHERE `Reference`
>> LIKE \'%%%02d-%%\' ORDER BY TRACK";
>>
>> for ($i = 1; $i < 100; $i++) {
>> $result = $mysql_query(sprintf($fmt,$i,$i)); if (!$result) {
>> // print an error message, maybe break
>> }
>> if (mysql_num_rows($result) == 0) break; // empty result set, finished
>> // process $result here
>> }
>>
>> ?>

> Cool! very many thanks, it works. One more question: why the double %% ?

Because in *printf format strings, % starts a format specifier, so you get:

%02d = integer with leading 0 if only 1 digit
%% = % symbol

and you want a % symbol in the output as a wildcard to LIKE

Thus, the format specifier "%%%02d-%%" when passed the number 3
generates the string "%03-%"

The %% in a *printf is like the \\ in a regex, you have to repeat the
escaping character to include it as a literal.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Question about binary transfers in POST requests
Next Topic: file_put_contents not creating file
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 23:30:40 GMT 2024

Total time taken to generate the page: 0.05241 seconds