Jump to content

dynamic variable assignment from query results


moonlightinred

Recommended Posts

Let's say there are 100 fields within a table and, instead of manually using $variable = $row['fieldname']; to assign each one, I'd like to dynamically take all of the results and create variables with the same field name.  For example, I have this:

 

$id = $_GET['id'];
$query = "SELECT * FROM table1 WHERE ID=$id";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
    $fname = $row['FNAME'];
    $lname = $row['LNAME'];
    $moddate = $row['MODDATE'];
}

 

except that it's a lot longer.  As you can see, I'm naming the variables as the lowercase equivalents of their field name within the table.  I'm sure there's an easy way to have it go through each field and assign the value to a variable, I just don't know what it is.  I would assume that, since I'm making the variables lowercase, it would be something like:

 

$strtolower($row['FNAME']) = $row['FNAME'];

 

but that doesn't work.  Suggestions?

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.