2
$username='Vasim';

$results = DB::select(
    'Select r.LocationId ,LM.Name+'-'+LM.LocationCode as LocationName
        from User_Master u(nolock), UserRoleLoc_link r(nolock)
        left join Location_Master LM(nolock)
        on r.LocationId = LM.LocationId
        Where r.UserId = u.UserId
        AND u.UserName = '$username' AND LM.Status = 1 AND
        (IsNull(NullIf('',''),'1')='1' Or r.LocationId = '') 
        AND LM.isLocationOnline = 1
        Group by r.UserId, u.UserName, 
            r.LocationId,u.Password,LM.Name,LM.LocationCode
        having COUNT(r.UserId) > 0
        Order By Count(r.UserId) DESC');

Error : syntax error, unexpected '$username' (T_VARIABLE)

how can i use my variable in this query

3
  • 1
    first thing it should be '.$username.' check concatination Commented Mar 11, 2016 at 13:53
  • Use "" instead of '' in your query. Commented Mar 11, 2016 at 13:53
  • Always fun when stackoverflow code syntax highlighting makes the problem quite clear! Commented Mar 11, 2016 at 13:54

2 Answers 2

2

Write your query with Double quote"" instead of single Quote''.

$results = DB::select("SELECT r.LocationId ,LM.Name+'-'+LM.LocationCode as LocationName From User_Master u(nolock), UserRoleLoc_link r(nolock) 
    LEFT JOIN Location_Master LM(nolock) on r.LocationId = LM.LocationId 
    WHERE r.UserId = u.UserId AND u.UserName = '$username' 
    AND LM.Status = 1 AND (IsNull(NullIf('',''),'1')='1' Or r.LocationId = '') 
    AND LM.isLocationOnline = 1 GROUP BY r.UserId, u.UserName, r.LocationId,u.Password,LM.Name,LM.LocationCode HAVING COUNT(r.UserId) > 0 
    ORDER BY Count(r.UserId) DESC");

PHP executes variable in double quotes not in single quote.

$username = 'vasim';
echo '$username';  // $username
echo "$username";  // vasim

Hope it will help you :-)

Sign up to request clarification or add additional context in comments.

Comments

2

Use your db query like the following string:

   $username='Vasim';
   $results = DB::select("Select r.LocationId ,LM.Name+'-'+LM.LocationCode as LocationName from User_Master u(nolock), UserRoleLoc_link r(nolock) left join Location_Master LM(nolock) 
                on r.LocationId = LM.LocationId
                Where r.UserId = u.UserId
                AND u.UserName = '".$username."'  AND LM.Status = 1 AND
                (IsNull(NullIf('',''),'1')='1' Or r.LocationId = '') AND LM.isLocationOnline = 1
                Group by r.UserId, u.UserName, r.LocationId,u.Password,LM.Name,LM.LocationCode
                having COUNT(r.UserId) > 0
                Order By Count(r.UserId) DESC");

Please check the double quoted string and concatenation change in the query there.

2 Comments

SQLSTATE[42S22]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'Vasim'. (SQL: Select r.LocationId ,LM.Name+'-'+LM.LocationCode as LocationName from User_Master u(nolock), UserRoleLoc_link r(nolock) left join Location_Master LM(nolock) on r.LocationId = LM.LocationId Where r.UserId = u.UserId AND u.UserName = Vasim AND LM.Status = 1 AND (IsNull(NullIf('',''),'1')='1' Or r.LocationId = '') AND LM.isLocationOnline = 1 Group by r.UserId, u.UserName, r.LocationId,u.Password,LM.Name,LM.LocationCode having COUNT(r.UserId) > 0 Order By Count(r.UserId) DESC)
@DeepKakkar: If you write query by concatation way then you should write UserName = '".$username."' instead of current one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.