Jump to content

Using list() with variables


apollo

Recommended Posts

The results are set by a variable (i.e $type = “pvt” , “com”, or “org”).
I am coding results from mysql to the LIST() function, I can not figure out how to construct a variable name within the list function. I was able to work it out in the mysql query…

This works
[code]$sql = "SELECT
".$type."_auto,
name_auto,
        ...
        FROM ".$db_prefix."rates ...
[/code]

However, in the list function it does not work as,

[code]list(
'$'.$type.'_auto'.,
$name_auto
    ...
) = mysql_fetch_array($rs);
[/code]

The variable name needs to be… $com_auto  in this case, but it could be $pvt_auto or $org_auto depending on the value of $type.

I have been stuck for awhile on this, maybe it can not be done this way using LIST to set the results from the database to be available as variables in the standard manner?

Thanks for any help here!
Apollo
Link to comment
Share on other sites

Part of your problem could be that you're using the mysql_fetch_array() function which returns two entries per field. You may want to try using the mysql_fetch_assoc() function instead.  Another part is how you're creating the variables, try this instead:
[code]<?php
list(${$type.'_auto'},$name_auto) = mysql_fetch_assoc($rs);
?>[/code]

But I believe a better solution would be something like this:
[code]<?php
$q = 'select ' . $type . '_auto, name_auto from ' . $db_prefix . 'rates';
$rs = mysql_query($q) or die('Problem with the query:$q<br>" . mysql_error());
$row = mysql_fetch_assoc($rs);
extract($row); // creates variables from the associative entries in the array
?>[/code]

Ken
Link to comment
Share on other sites

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.