Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - rename variable?

Options
  • 22-08-2007 3:32pm
    #1
    Closed Accounts Posts: 365 ✭✭


    Hi,

    I'm trying to create a function that will create its own variables based on values retrieved from a mysql database. i.e It creates a variable for each column in a table, and names the variable according to the column title. e.g If the column is headed "userid" the script will create a variable named "$userid" and assign it the value of that column for whichever particular row it is on in each iteration of a loop.

    I'm probably not explaining it very well! Am I way off here?

    [PHP]

    while ($row=mysql_fetch_array($query))
    {
    $result=mysql_list_fields("$db_name", "$table_name");
    $i=0;
    $num_fields=mysql_num_fields($result);

    while($i < $num_fields )
    {
    $var = mysql_field_name($result, $i);
    $$var = $row["$var"];
    $i++;
    }
    do_some_command;
    }


    [/PHP]


Comments

  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    Hmm... don't know exactly why you'd want to do what you're doing, but you could probably do it with the eval() function?

    If you want to do stuff like this, maybe you should look into technologies such as Active Record - maybe you should look at ADODB for inspiration.

    Hope this helps...
    Gadget


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    I asked this question last week on a different forum and I'll give you the same advice I got, 'You'd probably be better off by putting them into an array'.
    [php]
    $new_array=array();
    array_push($new_array,'Line no: '.$i)
    [/php]

    That should work, you can access them then by typing $new_array[1],$new_array[2] etc.

    This way means you can access them using a loop too.

    Hope I helped,
    S.


  • Closed Accounts Posts: 365 ✭✭ronanp


    Hmm... don't know exactly why you'd want to do what you're doing, but you could probably do it with the eval() function?

    Basically the application i'm creating is going to be fairly often querying the database, and assigning the data in it to various php variables, which have the same name as the table headers, for further use or manipulation.

    So i'm trying to make a generic function e.g:

    [PHP]
    void make_variables(string db_name, string table_name, string command], [string pattern, string field)
    [/PHP]

    So that I dont have to do the likes of this multiple times for multiple different values in multiple different tables:

    [PHP]
    $query=mysql_query("SELECT * FROM users WHERE user_id = '$user_id'") or die(mysql_error());

    while ($row=mysql_fetch_array($query))
    {
    $user_id = $row;
    $email = $row;
    $first_name = $row;
    $surname = $row;
    etc....

    }
    [/php]

    If you want to do stuff like this, maybe you should look into technologies such as Active Record - maybe you should look at ADODB for inspiration.

    Hope this helps...
    Gadget

    Will Do, cheers.
    Seachmall wrote:
    I asked this question last week on a different forum and I'll give you the same advice I got, 'You'd probably be better off by putting them into an array'.

    That might do it alright, cheers.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    ronanp wrote:
    So i'm trying to make a generic function e.g:

    [PHP]
    void make_variables(string db_name, string table_name, string command], [string pattern, string field)
    [/PHP]

    So that I dont have to do the likes of this multiple times for multiple different values in multiple different tables:

    [PHP]
    $query=mysql_query("SELECT * FROM users WHERE user_id = '$user_id'") or die(mysql_error());

    while ($row=mysql_fetch_array($query))
    {
    $user_id = $row;
    $email = $row;
    $first_name = $row;
    $surname = $row;
    etc....

    }[/PHP]
    Change mysql_fetch_array to mysql_fetch_assoc .
    In perl there is a FAQ item about creating variables on the fly. There it is recommended that you use a hash (aka an associative array).


  • Registered Users Posts: 1,262 ✭✭✭di11on


    Hi,

    Another way is to use the define function.

    You have a database called variables with two columns: var_key, var_value. Say,

    var_key
    var_value
    MY_PETS_NAME
    Rover


    Then you use the following function to create "defines" for every row of that table as follows:
    function set_values(){
       $variable_query = mysql_query('select var_key, var_value from variables);
       while ($variable = mysql_fetch_array($variable_query)) {
          define($variable['var_key'], $configuration['var_value']);
       } 
    }
    

    Then you can use:
    ...
    echo 'I have a pet called ' . MY_PETS_NAME;
    ...
    

    Hope this helps.


  • Advertisement
  • Registered Users Posts: 1,967 ✭✭✭Dun


    Are you not describing what the PHP extract function does?

    i.e:
    while ($row=mysql_fetch_array($query))
                    {
                     extract($row);
                     do_some_command;
                    } 
    


  • Closed Accounts Posts: 365 ✭✭ronanp


    Cheers for all the help lads, used eval in the end:

    [php]
    while($i < $num_fields )
    {
    $var = mysql_field_name($result, $i);
    eval('$' . $var . ' = ' . $row["$var"] . ';');
    $i++;

    }
    [/php]

    Dun wrote:
    Are you not describing what the PHP extract function does?

    i.e:
    while ($row=mysql_fetch_array($query))
                    {
                     extract($row);
                     do_some_command;
                    } 
    

    That looks like exactly what i'm trying to do, cheers, all those wasted hours!


Advertisement