On Wed, Mar 23, 2011 at 07:46:03AM +0000, Geoff Lane wrote:
> Hi Jim,
>
> On Wednesday, March 23, 2011, 1:42:18 AM, you wrote:
>
> > ok - here's the code in question.
> > $q = 'select * from director_records ';
> > $qrslt = mysql_query($q);
> > $rows = mysql_num_rows($qrslt);
> > for ($i=0; $i<$rows; $i++)
> > {
> > $j = $i+1;
> > $row = mysql_fetch_array($qrslt);
> > echo $j.'-'.$row['userid'];
> > if ($row['user_priv']<> "")
> > echo ' ('.$row['user_priv'].')
';
> > else
> > echo '
';
> > }
>
>
> > The output I get is:
>
>
> > 1-smith5
> > f-ginerjm (M)
> > g-smith8
>
> > While the alpha parts are valid, the index is only correct for the first
> > one
> > (0) obviously.
>
>
> I couldn't understand why you're getting characters, so I thought I'd
> have a go myself. First, some DDL and DML to recreate your data:
>
> create table director_records (userid char(16), user_priv char(8));
> insert into director_records (userid, user_priv) values ('smith5',
> ''),('ginerjm','M'),('smith8','');
>
> Now when I ran your code I got:
>
> 1-smith5
f-ginerjm (M)
g-smith8

>
> That is, all but the first result has 
x in front of it. These are
> HTML entities that display as characters and it so happens that f
> is 'j' and g is 'g'. Strictly, these entities should be terminated
> with a semi-colon (i.e. f and g), but your browser is
> 'obligingly' making sense of the 'bad formatting' and this is why
> you're getting characters.
>
> BTW, an alternative to your for construct would be to use a while loop
> to iterate through a data table. e.g. in your case, I'd have used:
>
> $q = 'select * from director_records ';
> $qrslt = mysql_query($q);
> $i = 1;
> while ($row = mysql_fetch_array($qrslt)){
> echo $i++ . '-' . $row['userid'];
> if ($row['user_priv']<>""){
> echo " (" . $row['user_priv'] . ")";
> }
> echo "<br>\n";
> }
>
> HTH,
*Brilliant* catch. Well done.
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php