Re: echo?

From: Date: Wed, 23 Mar 2011 07:46:03 +0000
Subject: Re: echo?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-311995@lists.php.net to get a copy of this message
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, -- Geoff Lane Cornwall, UK geoff@gjctech.co.uk

« previous php.general (#311995) next »