Variables in a select statement?
Moderator: General Moderators
Variables in a select statement?
I'm trying to write a page for users to create custom reports. They make their selections as to what columns, sorting, and selection criteria they want. I then pass these selections to another page that will display the report for them. Can you use variables in a select statement? i.e. $query="SELECT $c1, $c2, $c3,... FROM client WHERE lastname=... ORDER BY $s1, $s2, $s3... Can this be done? And how would you write the code? If this won't work do you have any suggestions.
Thanks!
Thanks!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- ReverendDexter
- Forum Contributor
- Posts: 193
- Joined: Tue May 29, 2007 1:26 pm
- Location: Chico, CA
Run the sql query outside of PHP, and make sure that it actually works.chull wrote:Yes, I did try them and they didn't seem to work. So I'm not sure whether you can use them or if my statement was written incorrectly.
If it does, make sure that your quoting is correct in the SQL, ie $int vs. '$string'
i alwsya is brackets
just in case i like to always use bracket in a statement like that
I think your lastname need to be quoted or any other character for that matter.
Code: Select all
$searchdb = "SELECT {$c1},{$c2}, {$c3}, {$c4} FROM client WHERE lastname="{$w1}" ORDER BY {$s1}, {$s2}, {$s3}";Re: i alwsya is brackets
yacahuma wrote:Code: Select all
$searchdb = "SELECT {$c1},{$c2}, {$c3}, {$c4} FROM client WHERE lastname="{$w1}" ORDER BY {$s1}, {$s2}, {$s3}";
It should be single quotes.
To the OP, one good idea is to echo your SQL statment instead of trying to run it. You can then look at the statement to see if it appears correct. That will give you some more info.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Well in that case you will need to do a little more building of the SQL statement. It will not be as easy as just passing in 4 values since one call could have 4 and the next could have 5 or 3. So you will need to have your sql something like thischull wrote:In this case Yes. However the number could increase or decrease depending on what information they need from the database.
Code: Select all
Select $selectfields
From client
Where lastname=$w1
Order by $orderfields