1

I have this function in a class where I have a condition that as an int param, without the contion where the query return corretly, but it this contiton it return the following error:

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@P1'

  function view_count_eventos(){

        $year=date('Y');
        try {
            $SQL = 'SELECT  Count(*)
            FROM [tablename] as j
            left join [tablename] as j1
            ON j.No_ = j1.No_
            where [Starting Date] like CONCAT('."%".':year'."%".')';
            $result = parent::connect()->prepare($SQL);
            $result->bindParam(':year', $year, PDO::PARAM_INT);
            $result->execute();
            $rs=$result->fetch();
            return $rs; 
        } catch (Exception $e) {
            die('Error Administrator(view_eventos) '.$e->getMessage());
        } finally{
            $result = null;
        }

    }
eshirvana
  • 23,227
  • 3
  • 22
  • 38
Luis Luis Maia Maia
  • 681
  • 1
  • 10
  • 30
  • 2
    The preferred way to use wildcards with bound parameters is to use `LIKE :year` and `$result->bindValue(':year', "%$year%");` – Nick Jan 23 '21 at 02:20
  • To work with your existing code, you need to fix your syntax error, it should be `CONCAT("%", :year, "%")'` – Nick Jan 23 '21 at 02:22
  • @Nick I tried your way, but I get the following error: `Cannot pass parameter 2 by reference` – Luis Luis Maia Maia Jan 23 '21 at 02:23
  • 1
    Sorry, that should have been `bindValue`, not `bindParam`; I edited the comment – Nick Jan 23 '21 at 02:24
  • Alternatively, use `$year = "%$year%"; $result->bindParam(':year', $year);` – Nick Jan 23 '21 at 02:25

0 Answers0