Jump to content

Can you use a variable text string in a WHILE loop


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

 

}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.