SQL in php script

Asked

Viewed 48 times

0

SELECT
CAST(REPLACE(CAST(DsXML as Nvarchar(Max)),' xmlns="http://www.portalfiscal.inf.br/nfe"','') as xml).value('(/nfeProc/NFe/infNFe/transp/vol/qVol/node())[1]', 'int') as [qVol]
FROM SPDNFE
WHERE CdIdNFe = 'NFe13161203976141000132550030000435291400513027'

When by this SQL query inside a php script, returns me the following error.

Parse error: syntax error, unexpected 'xmlns' (T_STRING)

How it is in PHP.

public function teste($teste) {
    $sql = 'SELECT
        CAST(REPLACE(CAST(DsXML as Nvarchar(Max)),' xmlns="http://www.portalfiscal.inf.br/nfe"','') as xml).value('(/nfeProc/NFe/infNFe/transp/vol/qVol/node())[1]', 'int') as [qVol]
        FROM SPDNFE
        WHERE CdIdNFe = '$teste'';
    $results = array();
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    if ($stmt) {
        while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
            $info = new Model();
            $info->getQVol($row->qVol);
            $results[] = $info;
        }
    }
    return $results;
}
  • 1

    Have you tried taking this space between ' xmlns...'?

  • Yes, but it’s part of the replace.

  • Test this way, please: 'SELECT
 CAST(REPLACE(CAST(DsXML as Nvarchar(Max)), " xmlns='http://www.portalfiscal.inf.br/nfe'","") as xml).value("(/nfeProc/NFe/infNFe/transp/vol/qVol/node())[1]", "int") as [qVol]
 FROM SPDNFE
 WHERE CdIdNFe = "'.$teste.'"'

1 answer

1


Use the exhaust of apostrophes:

$sql = "SELECT
        CAST(REPLACE(CAST(DsXML as Nvarchar(Max)),' xmlns=\"http://www.portalfiscal.inf.br/nfe\"','') as xml).value('(/nfeProc/NFe/infNFe/transp/vol/qVol/node())[1]', 'int') as [qVol]
        FROM SPDNFE
        WHERE CdIdNFe = \'$teste\'";

@Edit

When using the apostrophe to delimit your string, what you tried to do is interpreted as follows and delimited in the following part:

$sql =  'SELECT CAST(REPLACE(CAST(DsXML as Nvarchar(Max)),' 

You started a string with ' and then there is an occurrence of a ' where it should not mean the limit of string.

How do you want her string contain the literal, you must escape it.

See a similar example where the same error occurs:

$string = 'Santa Bárbara D'Oeste';

So that the literal ' function properly in the string, utilize \ to escape, or a function like addslashes:

$string = 'Santa Bárbara D\'Oeste';

$string = addslashes('Santa Bárbara D'Oeste');

I suggest you read on strings.

  • You can add more content about these exhausts ?

  • More content? You want an explanation about them, that’s it?

  • 1

    That’s right, if you can. After by the apostrophes returned me another error. Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]SELECT failed because the following SET options have incorrect settings: 'ARITHABORT'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.'

  • This second error no longer has any relation to the first, which is in your question. Ask a new question about the resolution of this second error.

  • I made an edit, please check. @Kevin. F

  • Perfect, now if you can help me with the other question here is the link https://answall.com/questions/227367/erro-em-query-sql-no-php

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.