Re: MySQL - field names

From: Date: Tue, 15 Aug 2000 11:43:34 +0000
Subject: Re: MySQL - field names
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11781@lists.php.net to get a copy of this message
o.cook@etoncollege.org.uk writes: > Typically when making a MySQL query I will do something similar to: > > $sql = "SELECT DISTINCT type FROM accttypes WHERE id=$requested_id"; > $result = mysql_query($sql,$conn); > while ( ($rs = mysql_fetch_array($result)) ) { > $accttype = $rs[0]; > } > > This strikes me as a bit clumsy, especially when lots of fields are returned > (I forever find myself counting forward from 0 to get the correct number for > a specific field!). I would much rather be able to access the record set > pieces by their field name? Something like $accttype = $rs.type as per the > above example? You're already using the right function - mysql_fetch_array, just use the fieldname instead of the number in your array like so: $sql = "SELECT DISTINCT type FROM accttypes WHERE id=$requested_id"; $result = mysql_query($sql,$conn); while ( ($rs = mysql_fetch_array($result)) ) { $accttype = $rs['type']; } -- Robin Vickery............................................... Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK Email: robin@planet-three.net Phone: +44 (0)794 670 6395

« previous php.general (#11781) next »