On 23 March 2011 07:46, Geoff Lane <ge...@gjctech.co.uk> 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,
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