Syntax error in mysql script [closed]

0
$consulta = DB::Select('
    SELECT nombrePerfilCargo, idPerfilCargo,porcentajeCargoHabilidad,porcentajeHabilidadCargo,c.Compania_idCompania
    FROM cargo c
    LEFT JOIN  cargohabilidad ch
    ON c.idCargo = ch.Cargo_idCargo
    LEFT JOIN perfilcargo pc
    ON ch.PerfilCargo_idPerfilCargo = pc.idPerfilCargo
    WHERE idCargo = '.$idCargo  and 'c.Compania_idCompania = '.\Session::get('idCompania'));

Can someone tell me what the syntax is like after the first condition of WHERE to add a second AND ...

WHERE idCargo = '.$idCargo  and 'c.Compania_idCompania = '.\Session::get('idCompania'));  

to add the Session::get ?

I'm getting a syntax error:

  

SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 (SQL: 1)

    
asked by Teo 01.03.2017 в 15:23
source

3 answers

3

It seems to me that you wrongly built the string of the query (you left the AND out). It should be like this:

$consulta = DB::Select(' SELECT nombrePerfilCargo, idPerfilCargo, porcentajeCargoHabilidad,
    porcentajeHabilidadCargo, c.Compania_idCompania
    FROM cargo c LEFT JOIN cargohabilidad ch ON c.idCargo = ch.Cargo_idCargo
    LEFT JOIN perfilcargo pc ON ch.PerfilCargo_idPerfilCargo = pc.idPerfilCargo 
    WHERE idCargo = '.$idCargo.' AND c.Compania_idCompania = '.\Session::get('idCompania'));
    
answered by 01.03.2017 / 15:29
source
2

I think the problem is in the construction with commas. I think that '.$idCargo and 'c.Compania_idCompania = ' should be '.$idCargo .'and c.Compania_idCompania = '

    
answered by 01.03.2017 в 15:26
2

Probably taking the AND from inside the string.

WHERE idCargo = '.$idCargo ' and c.Compania_idCompania = '.\Session::get('idCompania'))';
    
answered by 01.03.2017 в 15:27