note 68337 added to function.mysql-fetch-object

From: Date: Sun, 23 Jul 2006 23:55:29 +0000
Subject: note 68337 added to function.mysql-fetch-object
Groups: php.notes 
Request: Send a blank email to php-notes+get-115065@lists.php.net to get a copy of this message
This is a very very elegant (and costless) way to fetch an enterie query to every single field name from a "wide" table: The tedious way fetching: <?php $idtable=mysql_connect("localhost","user","pwd"); mysql_select_db("table",$idtable); $consult=mysql_query("SELECT * FROM models ORDER BY Serie,Year ASC"); while($row=mysql_fetch_object($consult)){ $IdLomo=$row->IdLomo; $Serie=$row->Serie; $Model=$row->Model; $Type=$row->Type; $Year=$row->Year; $Speed=$row->Speed; $Power=$row->Power; $Price=$row->Price; $Load1=$row->Load1; $Load2=$row->Load2; $Load3=$row->Load3; $Load4=$row->Load4; $Server=$row->Servier; $Real=$row->Real; $Lomo=$row->Lomo; } ?> Resulting on 15 new variables called like their name on table wich we introduced manualy for 5 minutes. But imagine each row has 100 fields! A way saving time with the same result: <?php $idtable=mysql_connect("localhost","user","pwd"); mysql_select_db("table",$idtable); $consult=mysql_query("SELECT * FROM models ORDER BY Serie,Year ASC"); // We find the fields number $numfields=mysql_num_fields($consult); // Now we put the names of fields in a Array for($i=0;$i<$numfields;$i++){ $fieldname[$i]=mysql_field_name($consult, $i); } while($row=mysql_fetch_object($consult)){ //Finally we assign the new variables for($i=0;$i<$numfields;$i++){ $$fieldname[$i]=$row->$fieldname[$i]; } } ?> ---- Server IP: 130.206.1.5 Probable Submitter: 84.77.141.61 ---- 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-object.php Edit -- http://master.php.net/note/edit/68337 Del: integrated -- http://master.php.net/note/delete/68337/integrated Del: useless -- http://master.php.net/note/delete/68337/useless Del: bad code -- http://master.php.net/note/delete/68337/bad+code Del: spam -- http://master.php.net/note/delete/68337/spam Del: non-english -- http://master.php.net/note/delete/68337/non-english Del: in docs -- http://master.php.net/note/delete/68337/in+docs Del: other reasons-- http://master.php.net/note/delete/68337 Reject -- http://master.php.net/note/reject/68337 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#115065) next »