note 66280 added to function.mysql-fetch-array

From: Date: Thu, 18 May 2006 10:39:54 +0000
Subject: note 66280 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-111577@lists.php.net to get a copy of this message
Just a fairly useful (to me at least!) "implementation" of mysql_fetch_assoc to stop the clobbering of identical column names and allow you to work out which table produced which result column when using a JOIN (or simple multiple-table) SQL query: (assuming a live connection ...) <?php $sql = "SELECT a.*, b.* from table1 a, table2 b WHERE a.id=b.id"; // example sql $r = mysql_query($sql,$conn); if (!$r) die(mysql_error()); $numfields = mysql_num_fields($r); $tfields = Array(); for ($i=0;$i<$numfields;$i++) { $field = mysql_fetch_field($r,$i); $tfields[$i] = $field->table.'.'.$field->name; } while ($row = mysql_fetch_row($r)) { $rowAssoc = Array(); for ($i=0;$i<$numfields;$i++) { $rowAssoc[$tfields[$i]] = $row[$i]; } // do stuff with $rowAssoc as if it was $rowAssoc = mysql_fetch_assoc($r) you had used, but with table. prefixes } ?> let's you refer to $rowAssoc['a.fieldname'] for example. [for real email addr, remove the ish] ---- Server IP: 84.18.202.180 Probable Submitter: 195.40.5.59 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- http://master.php.net/note/edit/66280 Del: integrated -- http://master.php.net/note/delete/66280/integrated Del: useless -- http://master.php.net/note/delete/66280/useless Del: bad code -- http://master.php.net/note/delete/66280/bad+code Del: spam -- http://master.php.net/note/delete/66280/spam Del: non-english -- http://master.php.net/note/delete/66280/non-english Del: in docs -- http://master.php.net/note/delete/66280/in+docs Del: other reasons-- http://master.php.net/note/delete/66280 Reject -- http://master.php.net/note/reject/66280 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111577) next »