RE: [PHP] MySQL - field names

From: Date: Tue, 15 Aug 2000 13:36:06 +0000
Subject: RE: [PHP] MySQL - field names
Groups: php.general 
Request: Send a blank email to php-general+get-11797@lists.php.net to get a copy of this message
Calling them in this way *seems* to be causing problems. For example: echo "<br>".$rs['type']; works correctly, whereas, echo "<br>$rs['type']"; results in this error: Parse error: parse error, expecting T_STRING' or T_VARIABLE' or `T_NUM_STRING' in /path/to/file.php3 on line 9 replacing $rs['type'] for $rs[0] (as before) makes both work. Am I missing something here? :) Ollie -----Original Message----- From: Robin Vickery [mailto:robin@planet-three.co.uk] Sent: 15 August 2000 12:44 To: php-general@lists.php.net Subject: Re: [PHP] MySQL - field names 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#11797) next »