Jump to content

[SOLVED] assign variable to mysql row (id = value)


Recommended Posts

i have a table in a mysql database that has two columns, id and value.  i would like to create a variable name out of each id and give it the corresponding value.  how do i do that?  below i have the script that will echo the text in th format i want, now if i can turn that echo into actually assigning a value to a variable name, i'll be set.

 

$result = mysql_query("SELECT * FROM reporting");

 

while ($row = mysql_fetch_assoc($result)) {

echo $row['id'] . " = " . $row['value'];

}

no i don't want to update anything in the database, the SELECT is correct.

 

i want to create a variable with the name that is equal to the contents of $row['id'] and then assign it the value of $row['value'].  i want it to do this for each row in the table.

OK you could do this

 

$id =$row['id'];

$$id = $row['value'];

 

BUT i assume that id is a number and a variable can NOT start with a number and even then you will probably end up in a mess with this..

 

may i ask why you wish to do this ?

i want to create a variable with the name that is equal to the contents of $row['id'] and then assign it the value of $row['value'].

 

Providing $row['id'] is not a number (variable names cannot be numbers) this will work for you.

 

<?php

  $result = mysql_query("SELECT * FROM reporting");

  while ($row = mysql_fetch_assoc($result)) {
    ${$row['id']} = $row['value'];
  }

?>

 

Why you would want to do this is behond me though.

i'm really amazed that this is not common.  i need to perform some math with the values in the database.  the id's are always the same, but the values UPDATED by another php script.  i want this script to fetch those values and assign a variable name to each of them so i can make some calculations to spit out in an excel spreadsheet.

 

is there a better way to accomplish this?

 

thanks a lot for these answers.  i haven't tried the cleaner one yet, but i'm quite sure it will work.

nope, not a fixed number of records.  the ids always stay the same, but there's a new one added every few weeks.

 

Then I really see no point in doing what your doing. This will meen you'll actually need to modify your code all the time. An example, say you want to display the id's a1 and a2.

 

<?php

  $result = mysql_query("SELECT * FROM reporting");

  while ($row = mysql_fetch_assoc($result)) {
    ${$row['id']} = $row['value'];
  }

  echo $a1;
  echo $a2;

?>

 

Now what happens when you want to echo a5? You need to modify your code. Makes no sense.

i don't get it.  why would i need to modify my code if a new row is added?  it's selecting everything in the table as a row, then assiging the values to a variable.  the name of the variable is $row['id'] and the value of that variable is $row['value'].  if a new row is added to the database, why wouldn't it fetch it too?

as i hit post i caught the misunderstanding here and now realize i did not explain this entirely. i like your profile avatar, so let me splain lucy.

 

so this table that's the topic o convo, is never echoed. i believe that's where you were thinking i would change my code, to add to the list that echos the results. it's all used for simple math computations, and with conformity in my naming convention of variables and id's, i can add rows to this table and it all work smoothly, without any code altering.

i would like to create a variable name out of each id and give it the corresponding value.

 

the name of the variable is $row['id'] and the value of that variable is $row['value'].

 

These two comments contridict each other.

 

mysql_fetch_assoc() returns an array. Each element of this array represents a field in your database. For each consecutive call to mysql_fetch_assoc() you get a new row. In your first post you said you wanted to 'create a variable name out of each id and give it the corresponding value'. You don't need to.

 

You really need to explain exactly what it is your wanting to do. Posting your code would also help.

 

Maybe all you really want is...

 

<?php

$result = mysql_query("SELECT * FROM reporting");

while ($row = mysql_fetch_assoc($result)) {
  $row['id'] = $row['value'];
}

?>

 

But even that makes little sense. Why not just do your calculations using $row['value'] instead of $row['id']?

my fault again, i forgot to mark this as solved (i'm usually really good about that too).  the two statements you quoted from me don't contradict each other.  that was me trying to verbalize what your code does.  problem solved, my bad.

 

anyweezer, it totally floors me that the usefullness is escaping you.  yes, to post my code would do much better than me trying to verbalize it, but in order to do that i have to do too much to it in order to make it acceptable for a public forum.  feel free to email if you'd like to discuss this further though.  thanks for your help!

If that last piece of code is seriously all you needed then instead you could simply use.

 

<?php

// you can assign names to your fields within the query.
$result = mysql_query("SELECT value AS id FROM reporting");

while ($row = mysql_fetch_assoc($result)) {
  // now $row['id'] is equivelent to what was $row['value'] in my previous post.
}

?>

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.