Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #11163 > unrolled thread

Will this work?

Started byDerek Turner <frderek@cesmail.net>
First post2011-02-07 23:03 +0000
Last post2011-02-08 08:36 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Will this work? Derek Turner <frderek@cesmail.net> - 2011-02-07 23:03 +0000
    Re: Will this work? Denis McMahon <denis.m.f.mcmahon@googlemail.com> - 2011-02-08 01:27 +0000
      Re: Will this work? Derek Turner <frderek@cesmail.net> - 2011-02-08 13:06 +0000
        Re: Will this work? Jerry Stuckle <jstucklex@attglobal.net> - 2011-02-08 08:36 -0500

#11163 — Will this work?

FromDerek Turner <frderek@cesmail.net>
Date2011-02-07 23:03 +0000
SubjectWill this work?
Message-ID<8rbc1jFh6iU2@mid.individual.net>
How long and complicated can a while condition be?

I want to:

define a string variable ($i) starting at "01"
use this in a mysql query
increment the string
dropout of the while when a zero result comes back from mysql

So, would something like:

while ($sql="some multi-lined mysql query containing $i twice" && 
$result=mysql($conn, $sql)){

do some stuff including incrementing $i;

}

presumably the first condition will always return true, will the second 
return false if no matches are found? Can a while condition run over 
several lines like this? The manual doesn't seem to say.

[toc] | [next] | [standalone]


#11226

FromDenis McMahon <denis.m.f.mcmahon@googlemail.com>
Date2011-02-08 01:27 +0000
Message-ID<4d509ce2$0$25004$bed64819@gradwell.net>
In reply to#11163
On 07/02/11 23:03, Derek Turner wrote:
> How long and complicated can a while condition be?
> 
> I want to:
> 
> define a string variable ($i) starting at "01"
> use this in a mysql query
> increment the string
> dropout of the while when a zero result comes back from mysql
> 
> So, would something like:
> 
> while ($sql="some multi-lined mysql query containing $i twice" && 
> $result=mysql($conn, $sql)){
> 
> do some stuff including incrementing $i;
> 
> }
> 
> presumably the first condition will always return true, will the second 
> return false if no matches are found? Can a while condition run over 
> several lines like this? The manual doesn't seem to say.

From the manual:

> For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
> resultset, mysql_query() returns a resource on success, or FALSE on
> error. 

Note that an empty result set is still a result set!

This might be neater, and allows you to use an integer for your loop:

$conn = mysql_connect($dbserver,$dbusername,$dbpassword);
$1 = 1;
$qryfmt = "select blah blah blah %02d blah blah blah %02d blah blah";

while ($result = mysql_query(sprintf($qryfmt,$i,$i),$conn) &&
mysql_num_rows($result) != 0) {
  // do stuff with result
  $i++;
}

[toc] | [prev] | [next] | [standalone]


#11256

FromDerek Turner <frderek@cesmail.net>
Date2011-02-08 13:06 +0000
Message-ID<8rctf4Fnl3U1@mid.individual.net>
In reply to#11226
On Tue, 08 Feb 2011 01:27:46 +0000, Denis McMahon wrote:

> Note that an empty result set is still a result set!
> 
> This might be neater, and allows you to use an integer for your loop:

<snip>

Thanks but I'm searching for a string like "02-" so $i needs to be a 
string.

$sql = "SELECT `workName`, `composer`, SUBSTRING(`Reference`,LOCATE(\'08-
\',`Reference`)) as TRACK FROM `work` WHERE `Reference` LIKE \'%08-%\' 
ORDER BY TRACK";

works fine but I want to search on "01-" ... "99-" stopping when I get a 
zero result. Thanks for the heads-up on zero-result != false, that, and 
your fix are very helpful.

[toc] | [prev] | [next] | [standalone]


#11281

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-02-08 08:36 -0500
Message-ID<iirgs2$d9e$2@news.eternal-september.org>
In reply to#11256
On 2/8/2011 8:06 AM, Derek Turner wrote:
> On Tue, 08 Feb 2011 01:27:46 +0000, Denis McMahon wrote:
>
>> Note that an empty result set is still a result set!
>>
>> This might be neater, and allows you to use an integer for your loop:
>
> <snip>
>
> Thanks but I'm searching for a string like "02-" so $i needs to be a
> string.
>
> $sql = "SELECT `workName`, `composer`, SUBSTRING(`Reference`,LOCATE(\'08-
> \',`Reference`)) as TRACK FROM `work` WHERE `Reference` LIKE \'%08-%\'
> ORDER BY TRACK";
>
> works fine but I want to search on "01-" ... "99-" stopping when I get a
> zero result. Thanks for the heads-up on zero-result != false, that, and
> your fix are very helpful.
>

Which is exactly what Dennis's code does.  Did you try it?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web