sql string + variable
Have "require" file containing several query stings.
Depending on user input one of strings is selected. Everything going
along smoothly until I wanted to also input a variable in string. If I
put string in program works ok, but, if I use string from require file I
can not seem to insert variable.
$cccb_id is sting..... to be inserted into $query4 and changes depending
on user input.
$query4 = "select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
',member_lname) as 'member' from member_cccb_lnk join member on
(member.member_no = member_cccb_lnk.member_no) join cccb on
member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = "$cccb_id"
order by member";
output is: select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
',member_lname) as 'member' from member_cccb_lnk join member on
(member.member_no = member_cccb_lnk.member_no) join cccb on
member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = order by
memberError 1064
as you can see, "$cccb_id" is not in query string.
any help will be appreciated.
Re: sql string + variable
user wrote:
> Have "require" file containing several query stings.
>
> Depending on user input one of strings is selected. Everything going
> along smoothly until I wanted to also input a variable in string. If I
> put string in program works ok, but, if I use string from require file I
> can not seem to insert variable.
>
> $cccb_id is sting..... to be inserted into $query4 and changes depending
> on user input.
>
> $query4 = "select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
> ',member_lname) as 'member' from member_cccb_lnk join member on
> (member.member_no = member_cccb_lnk.member_no) join cccb on
> member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = "$cccb_id"
> order by member";
>
You have to escape double quotes or replace them with single quotes. eg.
cccb.cccb_id = \"$cccb_id\" or cccb.cccb_id = '$cccb_id'
[snip]
>
> as you can see, "$cccb_id" is not in query string.
>
> any help will be appreciated.
Gvre
Re: sql string + variable
Nope. Have tried that and all kinds of other possibilities without
success. Think is has something to do with original sql statement being
in file and brought into current script with require function.
Thanks for the try.
Giannis Vrentzos wrote:
> user wrote:
>
>> Have "require" file containing several query stings.
>>
>> Depending on user input one of strings is selected. Everything going
>> along smoothly until I wanted to also input a variable in string. If I
>> put string in program works ok, but, if I use string from require file
>> I can not seem to insert variable.
>>
>> $cccb_id is sting..... to be inserted into $query4 and changes
>> depending on user input.
>>
>> $query4 = "select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
>> ',member_lname) as 'member' from member_cccb_lnk join member on
>> (member.member_no = member_cccb_lnk.member_no) join cccb on
>> member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = "$cccb_id"
>> order by member";
>>
>
> You have to escape double quotes or replace them with single quotes. eg.
> cccb.cccb_id = \"$cccb_id\" or cccb.cccb_id = '$cccb_id'
>
> [snip]
>
>>
>> as you can see, "$cccb_id" is not in query string.
>>
>> any help will be appreciated.
>
>
> Gvre
Re: sql string + variable
user <someone [at] someplace.org> wrote in
news:uIfrh.739360$QZ1.470012 [at] bgtnsc04-news.ops.worldnet.att. net:
> Have "require" file containing several query stings.
>
> Depending on user input one of strings is selected. Everything going
> along smoothly until I wanted to also input a variable in string. If I
> put string in program works ok, but, if I use string from require file
> I can not seem to insert variable.
>
> $cccb_id is sting..... to be inserted into $query4 and changes
> depending on user input.
>
> $query4 = "select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
> ',member_lname) as 'member' from member_cccb_lnk join member on
> (member.member_no = member_cccb_lnk.member_no) join cccb on
> member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = "$cccb_id"
> order by member";
>
> output is: select cccb.cccb_name as 'cccb', CONCAT(member_fname,'
> ',member_lname) as 'member' from member_cccb_lnk join member on
> (member.member_no = member_cccb_lnk.member_no) join cccb on
> member_cccb_lnk.cccb_id = cccb.cccb_id and cccb.cccb_id = order by
> memberError 1064
>
> as you can see, "$cccb_id" is not in query string.
>
> any help will be appreciated.
Try this:
$query4 = "select cccb.cccb_name as '_cccb', CONCAT(member_fname,'
',member_lname) as 'member' from member_cccb_lnk join member on
(member.member_no = member_cccb_lnk.member_no) join cccb on
member_cccb_lnk.cccb_id = cccb.cccb_id WHERE cccb.cccb_id = "$cccb_id"
order by member";
The selection you want to use (cccb.cccb_id = "$cccb_id") has nothing to
do with the join, so narrow your selection using the WHERE-clause. There
could also be a little confusion with the "AS 'cccb'", because cccb is
both a table as a fieldname. Try another fieldname instead (like
'_cccb').
--
Siebie
Re: sql string + variable
Also, try using sprintf() - http://us3.php.net/sprintf
If you don't know about formatted strings, it's a way to make a static
string, and put variables inside it, for instance:
sprintf("I have %d eggs in my %d baskets", $eggs, $basket); returns 'I
have 30 eggs in my 3 baskets'.
At first, it seems convoluted and extra work, but once you get used to
them, formatted strings are a /very/ powerful tool.