Page 2 of 2
Posted: Sun Apr 01, 2007 3:43 pm
by gfirman
If you look at the example above, I am querying a table with quantities of drugs stored for each user.
The user is using a random amount of one drug only. So the drug selected is stored in the variable and this is the column name. The returned value is the quantity of that drug the user has.
Posted: Sun Apr 01, 2007 3:54 pm
by JellyFish
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?
Posted: Sun Apr 01, 2007 4:04 pm
by gfirman
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"
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";
This is the result of the query
Code: Select all
lab drug: crystalmeth
lab drug quantity: 0
What I want to do is replace $crystalmeth with $labdrugquantity, but it doesn't work.........any ideas
Posted: Sun Apr 01, 2007 4:12 pm
by JellyFish
Try:
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";
hopefully that'll work?
Posted: Sun Apr 01, 2007 4:17 pm
by gfirman
Thanks a lot, that works. But can you tell me why the way I was doing it didn't work?
Posted: Sun Apr 01, 2007 4:29 pm
by JellyFish
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.

Posted: Sun Apr 01, 2007 4:32 pm
by gfirman
Alright thanks again. I'v only being php for 3 or 4 months now and I always used extract because I saw other people doing it. But thats a great help thanks!!!
Posted: Sun Apr 01, 2007 4:41 pm
by JellyFish
Cool. Good luck with your PHP experience.
TIP: Make sure you get the programmatic concepts down(i.e variables, arrays, operators, value-types, functions and then objects). It help me when I truly understand how everything everything worked, but I'm sure you'll get it all down.

Posted: Sun Apr 01, 2007 4:48 pm
by gfirman
thanks for the tip!
Posted: Sun Apr 01, 2007 5:56 pm
by ambivalent
JellyFish wrote:
Wait a minute. What are backticks used for MySQL syntax? I never known about them...
Check out
this forum post and
this post