Jump to content

Can you use a variable text string in a WHILE loop


beckjoh

Recommended Posts

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

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'];

 

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.