note 108848 modified in function.mysql-fetch-field by danbrown

From: Date: Wed, 30 May 2012 14:58:04 +0000
Subject: note 108848 modified in function.mysql-fetch-field by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188300@lists.php.net to get a copy of this message
Using mysql_fetch_field you can produce a more robust version of mysql_fetch_assoc.&#13;&#10;&#13;&#10;When querying 2 tables with the same field names, generally you would need to use mysql_fetch_row to get an integer key&#39;ed array rather than an associated key&#39;ed array. This is because fields of the same name in the second table will over-write the data returned from the first table.&#13;&#10;However this simple function will insert the table name prior to the field name for the key and prevent cross-overs.&#13;&#10;&#13;&#10;ie SELECT *, &#39;test&#39; AS test 4 FROM table AS T_1, table AS T_2 WHERE T_1.a=T_2.b&#13;&#10;&#13;&#10;could produce:&#13;&#10;&#13;&#10;mysql_fetch_assoc() returns&#13;&#10;array(&#13;&#10;&#39;index&#39;=&#62;2,&#13;&#10;&#39;a&#39;=&#62;&#39;pear&#39;,&#13;&#10;&#39;b&#39;=&#62;&#39;apple&#39;,&#13;&#10;&#39;test&#39;=&#62;&#39;test&#39;,&#13;&#10;4=&#62;4&#13;&#10;)&#13;&#10;&#13;&#10;mysql_fetch_table_assoc() returns&#13;&#10;array(&#13;&#10;&#39;T_1.index&#39; =&#62;1,&#13;&#10;&#39;T_1.a&#39;=&#62;&#39;apple&#39;,&#13;&#10;&#39;T_1.b&#39;=&#62;&#39;banana&#39;,&#13;&#10;&#39;T_2.index&#39;=&#62;2,&#13;&#10;&#39;T_2.a&#39;=&#62;&#39;pear&#39;,&#13;&#10;&#39;T_2.b&#39;=&#62;&#39;apple&#39;,&#13;&#10;&#39;test&#39;=&#62;&#39;test&#39;,&#13;&#10;4=&#62;4&#13;&#10;)&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;function mysql_fetch_table_assoc($resource)&#13;&#10;{&#13;&#10; // function to get all data from a query, without over-writing the same field&#13;&#10; // by using the table name and the field name as the index&#13;&#10; &#13;&#10; // get data first&#13;&#10; $data=mysql_fetch_row($resource);&#13;&#10; if(!$data) return $data; // end of data&#13;&#10; &#13;&#10; // get field info&#13;&#10; $fields=array();&#13;&#10; $index=0;&#13;&#10; $num_fields=mysql_num_fields($resource);&#13;&#10; while($index&#60;$num_fields)&#13;&#10; {&#13;&#10; $meta=mysql_fetch_field($resource, $index);&#13;&#10; if(!$meta)&#13;&#10; {&#13;&#10; // if no field info then just use index number by default&#13;&#10; $fields[$index]=$index;&#13;&#10; }&#13;&#10; else&#13;&#10; {&#13;&#10; $fields[$index]=&#39;&#39;;&#13;&#10; // deal with field aliases - ie no table name (SELECT T_1.a AS temp)&#13;&#10; if(!empty($meta-&#62;table)) $fields[$index]=$meta-&#62;table.&#39;.&#39;;&#13;&#10; // deal with raw data - ie no field name (SELECT 1)&#13;&#10; if(!empty($meta-&#62;name)) $fields[$index].=$meta-&#62;name; else $fields[$index].=$index;&#13;&#10; }&#13;&#10; $index++;&#13;&#10; }&#13;&#10; $assoc_data=array_combine($fields, $data);&#13;&#10; return $assoc_data;&#13;&#10;}&#13;&#10;?&#62; --was-- Using mysql_fetch_field you can produce a more robust version of mysql_fetch_assoc. When querying 2 tables with the same field names, generally you would need to use mysql_fetch_row to get an integer key'ed array rather than an associated key'ed array. This is because fields of the same name in the second table will over-write the data returned from the first table. However this simple function will insert the table name prior to the field name for the key and prevent cross-overs. ie SELECT *, 'test' AS test 4 FROM table AS T_1, table AS T_2 WHERE T_1.a=T_2.b could produce: mysql_fetch_assoc() returns array( 'index'=>2, 'a'=>'pear', 'b'=>'apple', 'test'=>'test', 4=>4 ) mysql_fetch_table_assoc() returns array( 'T_1.index' =>1, 'T_1.a'=>'apple', 'T_1.b'=>'banana', 'T_2.index'=>2, 'T_2.a'=>'pear', 'T_2.b'=>'apple', 'test'=>'test', 4=>4 ) function mysql_fetch_table_assoc($resource) { // function to get all data from a query, without over-writing the same field // by using the table name and the field name as the index // get data first $data=mysql_fetch_row($resource); if(!$data) return $data; // end of data // get field info $fields=array(); $index=0; $num_fields=mysql_num_fields($resource); while($index<$num_fields) { $meta=mysql_fetch_field($resource, $index); if(!$meta) { // if no field info then just use index number by default $fields[$index]=$index; } else { $fields[$index]=''; // deal with field aliases - ie no table name (SELECT T_1.a AS temp) if(!empty($meta->table)) $fields[$index]=$meta->table.'.'; // deal with raw data - ie no field name (SELECT 1) if(!empty($meta->name)) $fields[$index].=$meta->name; else $fields[$index].=$index; } $index++; } $assoc_data=array_combine($fields, $data); return $assoc_data; } http://php.net/manual/en/function.mysql-fetch-field.php

« previous php.notes (#188300) next »