Select $variable
Moderator: General Moderators
I still don't understand what you're trying to do to well. As for your problem, if you want to assign a value to a variable just use:
that is, if this is your problem. What's your problem?
EDIT: Oh and what's your current result?
Code: Select all
$variable = "value";
EDIT: Oh and what's your current result?
Ok so this query works, but only because the variable holding the column name is the same as the variable that it is assigned to.
$lab drug holds "crystalmeth"
This is the result of the query
What I want to do is replace $crystalmeth with $labdrugquantity, but it doesn't work.........any ideas
$lab drug holds "crystalmeth"
Code: Select all
echo "lab drug: $labdrug<br>";
//Selecting from drug table
$sqldrugs = "SELECT `$labdrug` FROM `userdrugs` WHERE id='$drugid'";
$resdrugs = mysql_query($sqldrugs,$db)
or die("Couldn't execute query1.");
while($rowdrugs = mysql_fetch_array($resdrugs))
{
extract($rowdrugs);
$crystalmeth;
}
echo "lab drug quantity: $crystalmeth";Code: Select all
lab drug: crystalmeth
lab drug quantity: 0What I want to do is replace $crystalmeth with $labdrugquantity, but it doesn't work.........any ideas
Try:
hopefully that'll work?
Code: Select all
echo "lab drug: $labdrug<br>";
//Selecting from drug table
$sqldrugs = "SELECT `$labdrug` FROM `userdrugs` WHERE id='$drugid'";
$resdrugs = mysql_query($sqldrugs,$db)
or die("Couldn't execute query1.");
while($rowdrugs = mysql_fetch_array($resdrugs))
{
$labdrugquantity = $rowdrugs[0];
}
echo "lab drug quantity: $labdrugquantity"; Sure. Basically the way you were doing it didn't work because you were creating variables from an array with names you do not know and vary.
By using extract() the created variable names are unknown to you. The extract() function has it's moments of use and this instance isn't one of them.
What you wanted to do is create a variable with a name you know. The problem was you didn't know how to access the value for it. The value was $rowdrugs[0], the first index placement of the $rowdrugs array.
I think this is what Oren was saying when he said "...I don't see why you use extract..." the use of extract just didn't fall into play in this scenario.
By using extract() the created variable names are unknown to you. The extract() function has it's moments of use and this instance isn't one of them.
What you wanted to do is create a variable with a name you know. The problem was you didn't know how to access the value for it. The value was $rowdrugs[0], the first index placement of the $rowdrugs array.
I think this is what Oren was saying when he said "...I don't see why you use extract..." the use of extract just didn't fall into play in this scenario.
- ambivalent
- Forum Contributor
- Posts: 173
- Joined: Thu Apr 14, 2005 8:58 pm
- Location: Toronto, ON
Check out this forum post and this postJellyFish wrote: Wait a minute. What are backticks used for MySQL syntax? I never known about them...