Copy link to clipboard
Copied
Hello All,
Quick one for you:
Let's say that I have several columns in a table with names such as subject_1, subject_2, subject_3, etc. The table's name is subject_names.
The number in each of the three column name examples is also a value passed along a query string, the user can select choices, 1, 2 or 3. That query string's variable is $qs.
So, what I want is a SELECT query that uses the query string value as follows (KEEP IN MIND, I know this is not the proper syntax):
"SELECT subject_[$qs]
FROM subject_names";
I have tried all sorts of cominations of quotes (single and double), dots, brackets, braces and parenthesis. I just want to know how to include such a variable within this code.
Any and all help is sincerely appreciated!
Cheers,
wordman
Copy link to clipboard
Copied
If you are using PHP I can help you with this. Otherwise you may need some slightly different instructions.
When the user enters their choice and submits the form, you would have it picked up by the $_POST variable (you could use the $_GET variable, but it seems more common to use the $_POST). You can set any variable you want to that in the $_POST array.
Ex: $qs = $_POST['choice_field_name'];
Then you can manipulate your query with the newly set variable.
Ex:
$query = 'SELECT '.$qs'.' FROM subject_names';
That should work, if you are using PHP to connect to your database. I am not familiar with ASP or JSP, or others. Although, SQL should be pretty similar accross the different scripting languages and their popular database management systems.
Hope that helps.
Copy link to clipboard
Copied
Thanks, but that's not what I meant. I'm not using a form, just a numerical variable from a query string.
I know how to transfer variables. I meant, what is the proper syntax to include the variable in my php-based SELECT query. Syntax as in, what series of dots/brackets/braces, etc. there is no clear-cut syntax guide out there.
To wit:
Instead of hard coding:
"SELECT subject_1
FROM subject_names";
I want the number 1 above replaced with a variable. Again, I'm good with how to pass it via query string, I simply want to complete subject_(either1, 2 or 3) with the variable. So, how do I enclose the variable in the code? that's all. I don't need to pass along the whole name 'subject_1, etc., I just want the number added as part of the query.
I've gone a different route with my database and coding, but still, this sort of syntax pops up at times and would be useful to know.
Thanks for the detailed answer though. Often times it's hard for me to explain my question optimally, I hope this helped.
Cheers!!!
wordman
Copy link to clipboard
Copied
Well, I did give you the syntax though.
$query = 'SELECT ' . $qs . ' FROM tbl_name';
I put spaces between the periods this time to make it more clear.
If you put the actual word 'subject' in there and just want your form to name it's options as the numbers available you could do this:
$query = 'SELECT subject_' . $qs . ' FROM tbl_name';
In PHP you can use either single or double quotes around your query string, I always just use single quotes. I see a lot of other use double quotes.
Double quotes would look like:
$query = "SELECT subject_' . $qs . ' FROM tbl_name";
Or when using double quotes you can actually just place the variable right in the string without having to concatenate multiple strings like above.
Since you mentioned that you are good with passing variables I probably don't have to mention that you need to set the value attribute of your option tags (if you are using a select) to the value you want them to pass.
Ex:
<select name="choices">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If you have that part all figured out then you can use the syntax above for your query string.
Good luck.
Copy link to clipboard
Copied
>Let's say that I have several columns in a table with
>names such as subject_1, subject_2, subject_3, etc.
Then I would say you have not designed the database properly
Seriously, this looks like a case for further normalization.
>The number in each of the three column name examples is
>also a value passed along a query string, the user can select
>choices, 1, 2 or 3. That query string's variable is $qs.
This is further evidence of a design problem.
Copy link to clipboard
Copied
Gentlemen,
Thank you both for your replies. I apologise for not being able to clarify myself, and I now WITHDRAW any further interest in this topic.
Bregent, this has nothing to do with database design or nornalization. I SHOULD have worded my question: 'Can this be done?'
I'm simply curious.
Ute, again, thanks, but that wasn't what I was looking for. I already know how to do that.
My sincere apologies for not being able to spell this out clearer. Unfortunately, there are times, when one's knowledgebase isn't skilled enough to compose a question about the very thing we need to learn next.
On closing, I am not using this approach, I am on a different tack and simply wanted to know if a particular syntax existed.
Again, thanks to you both!
Cheers,
wordman
PS - if a moderator can delete this thread, please do.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now