PHP PDOStatement::rowCount problem

%3CLINGO-SUB%20id%3D%22lingo-sub-2668330%22%20slang%3D%22en-US%22%3EPHP%20PDOStatement%3A%3ArowCount%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2668330%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%3C%2FP%3E%3CP%3EI%20have%20a%20problem%20with%20the%20rowCount()%20in%20this%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-php%22%3E%3CCODE%3E%24sqlLoc%3D%20%22DECLARE%20%40Data2%20AS%20DATE%3B%20%0A%20%20%20%20%20%20%20%20%20%20SET%20%40Data2%20%3D%20CONVERT(DATE%2CCONVERT(date%2C%20'%24dataIncasso')%2C%20102)%3B%0A%20%20%20%20%20%20%20%20%20%20DECLARE%20%40Data1%20AS%20DATE%3B%0A%20%20%20%20%20%20%20%20%20%20SET%20%40Data1%20%3D%20DATEADD(DAY%2C%20%24interval%2C%20%40Data2)%0A%20%20%20%20%20%20%20%20%20%20SELECT%20noteincassi.CodLocale%2CInsegna%2CCitta%0A%20%20%20%20%20%20%20%20%20%20FROM%20%5BEdera%5D.%5Bdbo%5D.%5BNoteIncassi%5D%2Cedera.dbo.AnagraficaLocali%0A%20%20%20%20%20%20%20%20%20%20where%20DataIncasso%3D%40Data2%20AND%20tipoincasso%3D'6'%20and%20AnagraficaLocali.CodLocale%3DNoteIncassi.CodLocale%20AND%20sospeso%3D0%0A%20%20%20%20%20%20%20%20%20%20GROUP%20BY%20noteincassi.CodLocale%2Cinsegna%2CCitta%0A%20%20%20%20%20%20%20%20%20%20ORDER%20BY%20Insegna%22%3B%0A%24queryLoc%3D%24conn-%26gt%3Bprepare(%24sqlLoc%2Carray(PDO%3A%3AATTR_CURSOR%20%3D%26gt%3B%20PDO%3A%3ACURSOR_SCROLL))%3B%0A%2F%2F%24queryLoc-%26gt%3Bexecute()%3B%0A%20if(%24queryLoc-%26gt%3Bexecute())%7B%0Aecho%20%24numero%3D%24queryLoc-%26gt%3BrowCount()%3B%0A%20%7D%0A%24id%3D0%3B%0Awhile(%24resultLoc%3D%24queryLoc-%26gt%3Bfetch())%7B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20will%20print%20-1%20but%20the%20fecth%20is%20working%20and%20row%20are%20returned%2Cso%20the%20rest%20of%20the%20code%20is%20working%20fine%2C%20it's%20only%20the%20rowcount%20and%20I%20don't%20understand%20why.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2668330%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Epdo%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ephp%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ErowCount%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Esqlserver%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hello everyone,

I have a problem with the rowCount() in this code:

 

$sqlLoc= "DECLARE @Data2 AS DATE; 
          SET @Data2 = CONVERT(DATE,CONVERT(date, '$dataIncasso'), 102);
          DECLARE @Data1 AS DATE;
          SET @Data1 = DATEADD(DAY, $interval, @Data2)
          SELECT noteincassi.CodLocale,Insegna,Citta
          FROM [Edera].[dbo].[NoteIncassi],edera.dbo.AnagraficaLocali
          where DataIncasso=@Data2 AND tipoincasso='6' and AnagraficaLocali.CodLocale=NoteIncassi.CodLocale AND sospeso=0
          GROUP BY noteincassi.CodLocale,insegna,Citta
          ORDER BY Insegna";
$queryLoc=$conn->prepare($sqlLoc,array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
//$queryLoc->execute();
 if($queryLoc->execute()){
echo $numero=$queryLoc->rowCount();
 }
$id=0;
while($resultLoc=$queryLoc->fetch()){

 

It will print -1 but the fecth is working and row are returned,so the rest of the code is working fine, it's only the rowcount and I don't understand why.

 

Thank you

 

1 Reply
I don't see why since it is php,
at least you can fake it by adding [count() over ] to select statement and use it for purpose as it be.