Re: Re: echo?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 23 March 2011 07:46, Geoff Lane <geoff@xxxxxxxxxxxxx> 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'].')&#13&#10';
>> Â Â else
>> Â Â Â Â echo '&#13&#10';
>> Â Â }
>
>
>> 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&#13&#102-ginerjm (M)&#13&#103-smith8&#13&#10
>
> That is, all but the first result has &#10x in front of it. These are
> HTML entities that display as characters and it so happens that &#102
> is 'j' and &#103 is 'g'. Strictly, these entities should be terminated
> with a semi-colon (i.e. &#102; and &#103;), 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,

I use ...

while(False !== ($row = mysql_fetch_array($qrslt)){
}

just so that if I have a query with 1 cell which is 0, or '', I don't
abort the loop.


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux