Select $variable
Moderator: General Moderators
Select $variable
Hi,
I'm doing an sql select statement and need to select a particular column from a table. The column name is stored in a variable. Is this possible.
This is what I'm trying to do:
SELECT $drug FROM userdrug WHERE id='$drugid'
Does anyone know if this is possible or is there a way around it?
I'm doing an sql select statement and need to select a particular column from a table. The column name is stored in a variable. Is this possible.
This is what I'm trying to do:
SELECT $drug FROM userdrug WHERE id='$drugid'
Does anyone know if this is possible or is there a way around it?
Last edited by gfirman on Sun Apr 01, 2007 2:32 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
echo out the query string to see exactly what the outcome is, and post it here.
You might also want to consider that the variable is indeed a valid column name.. Perhaps have an array of valid column names and check the variable using in_array(). Another thing, you might want to wrap your column names in backticks to avoid reserved name error.
Also, what does mysql_error() tell you? (If you don't know how to use mysql_error -- mysql_query() or die(mysql_error()))
You might also want to consider that the variable is indeed a valid column name.. Perhaps have an array of valid column names and check the variable using in_array(). Another thing, you might want to wrap your column names in backticks to avoid reserved name error.
Also, what does mysql_error() tell you? (If you don't know how to use mysql_error -- mysql_query() or die(mysql_error()))
Last edited by John Cartwright on Sun Apr 01, 2007 2:37 pm, edited 2 times in total.
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Ok I know what the problem is. I'v had this problem before and I don't know how to get around it. Can you guys help.
This piece of code works only because when i'm assigning the value to a variable, the name of the variable must be the column name which is selected. crystalmeth is a column name, but there are many others. How do i assign a value to a different variable name?Code: Select all
//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";feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]First, please use the PHP tags when you post your code.
Second, I don't see why you use extract and I also don't get what the second line in your while loop is for?!?!
By the way, what is the output of this code when you try to run it? Do you see on the screen: Couldn't execute query1. or something else?
Second, I don't see why you use extract and I also don't get what the second line in your while loop is for?!?!
By the way, what is the output of this code when you try to run it? Do you see on the screen: Couldn't execute query1. or something else?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Have you read my last post?
But anyway. What else could I use instead of extract? This is just what I use the whole time. I have never knew there was another way to do it. And as for the while loop, the same thing again. Its just what I saw other people using so I use it now as well.
Is there a better way to do it?
But anyway. What else could I use instead of extract? This is just what I use the whole time. I have never knew there was another way to do it. And as for the while loop, the same thing again. Its just what I saw other people using so I use it now as well.
Is there a better way to do it?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I'm not going to guess why your things arn't working.. so if you can't provide the details I asked I won't be able to help you.
As for the backticks.. they are `
Also, instead of extract simply reference the column by using the array indice.
As for the backticks.. they are `
Code: Select all
SELECT `column` FROM `table` Code: Select all
$rowdrugs['column_name_goes_here']Wait a minute. What are backticks used for MySQL syntax? I never known about them...Jcart wrote:I'm not going to guess why your things arn't working.. so if you can't provide the details I asked I won't be able to help you.
As for the backticks.. they are `
Also, instead of extract simply reference the column by using the array indice.Code: Select all
SELECT `column` FROM `table`
Code: Select all
$rowdrugs['column_name_goes_here']
Em, I don't understand. I told you that there is no problem with the column name in the $labdrug variable. What else were you asking?
Anyway I'v been trying assign the value to a variable the way you showed me, but can't get it to work. Could you show me an example of how it works.
I have also used the tickbacks and they didn't do anything.
Anyway I'v been trying assign the value to a variable the way you showed me, but can't get it to work. Could you show me an example of how it works.
I have also used the tickbacks and they didn't do anything.