beckjoh Posted February 3, 2009 Share Posted February 3, 2009 I am trying to do this: $result = mysql_query("SELECT * FROM table"); while($myrow=mysql_fetch_array($result)) { $var1 = $myrow['column1']; $var2 = $myrow['column2']; $var3 = $myrow['column3']; } With this: $pre_query = "SELECT * FROM $table"; //to get field names $result = mysql_query($pre_query) or die(mysql_error()); $dollar = "$"; $myrow = "myrow"; for($i = 0; $i < mysql_num_fields($result); $i++) { if ($i == (mysql_num_fields($result)-1)) $math .= "$".mysql_field_name($result,$i)." = ".$dollar.$myrow."['".mysql_field_name($result,$i)."'];<br>"; else $math .= "$".mysql_field_name($result,$i)." = ".$dollar.$myrow."['".mysql_field_name($result,$i)."'];<br>"; } $result = mysql_query("SELECT * FROM table"); while($myrow=mysql_fetch_array($result)) { $math; } CAN IT BE DONE? THANKS Link to comment https://forums.phpfreaks.com/topic/143692-can-you-use-a-variable-text-string-in-a-while-loop/ Share on other sites More sharing options...
Maq Posted February 3, 2009 Share Posted February 3, 2009 1) Can you elaborate on your question, it's very unclear. 2) Use code tags. Link to comment https://forums.phpfreaks.com/topic/143692-can-you-use-a-variable-text-string-in-a-while-loop/#findComment-753948 Share on other sites More sharing options...
beckjoh Posted February 4, 2009 Author Share Posted February 4, 2009 I am trying to set $var1, $var2, and $var3 with this code. However, I do not always know the names of the columns or the number of columns, so I wrote some code to pull out the field names and number of fields and I was trying to use them to accomplish this task. Actually I would like to set $column1_name = $myrow['column1'], etc. $result = mysql_query("SELECT * FROM table"); while($myrow=mysql_fetch_array($result)) { $var1 = $myrow['column1']; $var2 = $myrow['column2']; $var3 = $myrow['column3']; } Link to comment https://forums.phpfreaks.com/topic/143692-can-you-use-a-variable-text-string-in-a-while-loop/#findComment-754068 Share on other sites More sharing options...
fenway Posted February 6, 2009 Share Posted February 6, 2009 However, I do not always know the names of the columns or the number of columns Why not?? Link to comment https://forums.phpfreaks.com/topic/143692-can-you-use-a-variable-text-string-in-a-while-loop/#findComment-755795 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.