Select $variable

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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:

Code: Select all

$variable = "value";
that is, if this is your problem. What's your problem?


EDIT: Oh and what's your current result?
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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?
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post by gfirman »

Thanks a lot, that works. But can you tell me why the way I was doing it didn't work?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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. ;)
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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!!!
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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. :D
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post by gfirman »

thanks for the tip!
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post 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
Post Reply