title: mssql_query(): message: Incorrect syntax near ''(severity 15)
body: <p>when I run my code i get this error:</p>
  <blockquote>
  <p>Warning: mssql_query(): message: Incorrect syntax near 'bstplanning'.
  (severity 15) in /var/www/bstplanning/report/scorecard2.php on line 91</p>
  <p>Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 15) in
  /var/www/bstplanning/report/scorecard2.php on line 91</p>
  </blockquote>
  <p>My code looks like this:</p>
  <pre><code>    if ($team != 'All') {
      $sql_team = &quot;users.split = '&quot; . $team . &quot;' AND&quot;;
      $sql_user = &quot;users.user_id LIKE '%'&quot;;
  } else {
      $sql_team = &quot;users.user_id LIKE '%' AND&quot;;
      if ($user != 'All') {
          $sql_user = &quot;users.user_id = '&quot; . $user . &quot;'&quot;;
      } else {
          $sql_user = &quot;users.user_id LIKE '%'&quot;;
      }
  }
  
  $sql = &quot;
  SELECT 
          Drive.Owner 
          ,ROUND(AVG(CAST(actual AS FLOAT)), 2) as rezult
          ,ROUND(Sum(CAST(Ontime AS FLOAT))/Sum(task_count), 2) as rezult2
         ,ROUND(sum(case when firsta != 'none' then 1 else 0 end)/ROUND(CAST(count(firsta)AS FLOAT),2)*100,2) nones1
         ,ROUND(sum(case when seconda != 'none' then 1 else 0 end)/ROUND(CAST(count(seconda)AS FLOAT),2)*100,2) nones1
         ,ROUND(CAST(count(dock )AS FLOAT),2)/CAST(sum(case when dock = 'yes' then 1 else 0 end)AS FLOAT)*100 as documentavail
         ,(sum(case when dock = 'yes' then 1 else 0 end)+sum(case when dock = 'old' then 1 else 0 end))/count(dock)*100 as documentavail
  FROM (
  SELECT 
      users.user_name+ ' ' +users.user_surname AS Owner
      ,users.split as test
      ,sc.firstbackup as firsta
      ,sc.secondbackup as seconda
      ,sc.documentation as dock
      ,sc.active as active
      ,(SELECT CASE WHEN non_sc = '1' THEN 'regular' ELSE 'sc' END) as sc_or_non_sc
      FROM
      sc
  
  INNER JOIN
      users
  ON sc.user_id = users.user_id
  
  WHERE
       &quot; . $sql_team . &quot;  &quot; . $sql_user . &quot;
  ) Drive
  left join
  (
  SELECT
       bstplanning.dbo.sc_data.track
      ,bstplanning.dbo.users.split
      ,bstplanning.dbo.sc_data.country
      ,bstplanning.dbo.sc_data.client
      ,bstplanning.dbo.sc_data.task_group
      ,MAX(ISNULL(bstplanning.dbo.users.user_name,'') + ' ' + ISNULL(bstplanning.dbo.users.user_surname,'')) as Owner
      ,AVG(CASE WHEN bstplanning.dbo.sc_data.ontime = 'on time' THEN 100 ELSE 0 END)*COUNT(bstplanning.dbo.sc_data.country) AS Ontime
      ,AVG(CASE WHEN bstplanning.dbo.sc_data.accuracy = 'accurate' THEN 100 ELSE 0 END) AS actual
      ,COUNT(bstplanning.dbo.sc_data.country) AS task_count
  
  FROM 
      bstplanning.dbo.sc_data 
  
  INNER JOIN bstplanning.dbo.users 
      ON bstplanning.dbo.sc_data.user_id = bstplanning.dbo.users.user_id  
  
  WHERE
      bstplanning.dbo.sc_data.date &gt;= '&quot; . $nuo . &quot;' AND bstplanning.dbo.sc_data.date &lt;= '&quot; . $iki . &quot;' AND  &quot; . $sql_team . &quot; &quot; . $sql_user . &quot; bstplanning.dbo.sc_data.actual &gt; 0 AND
      ((bstplanning.dbo.sc_data.ontime='on time' OR bstplanning.dbo.sc_data.ontime = 'late') OR (bstplanning.dbo.sc_data.accuracy='accurate' OR bstplanning.dbo.sc_data.accuracy = 'error'))
      
  GROUP BY
       bstplanning.dbo.sc_data.country
      ,bstplanning.dbo.sc_data.client
      ,bstplanning.dbo.sc_data.task_group
      ,bstplanning.dbo.users.split
      ,bstplanning.dbo.sc_data.track
      ) Drive2
      on Drive.Owner=Drive2.Owner
      where sc_or_non_sc ='sc'
      group by
          Drive.Owner
  &quot;;
  </code></pre>
  <p>I don't know whats wrong because when i run code in SQL Server Management it works fine. I think problem is with $team or $user, but i cant figure how to fix it. This is crazy, when i remove &quot;Where&quot; from select it works.</p>