简体   繁体   中英

SELECT in Php with PDO (dblib) to SQL view

I am trying to execute a SQL query and it does not work.

$sql = "SELECT top 5 AccountDocument from web.Preoffers_new where AccountDocument = 'xxxxxxxx'";
$stmt = $db->prepare($sql);
$stmt->execute();

if ($data = $stmt->fetch()) {
    do {
        echo $data['AccountDocument'] . '<br>';
    } while ($data = $stmt->fetch());
} else {
    echo 'Empty Query';
}

This works perfectly and shows me the results.

But this:

$sql = "SELECT top 5 Document from Companies where Document = 'xxxxxxxx'";
$stmt = $db->prepare($sql);
$stmt->execute();

if ($data = $stmt->fetch()) {
    do {
        echo $data['Document'] . '<br>';
    } while ($data = $stmt->fetch());
} else {
    echo 'Empty Query';
}

Go directly to Empty Query. But is not true because if I execute that query in SQL it works perfectly.

The only diferent i see is the first one query go to a table in the DB and the second one go to a one view...

Some clues could be this?

    SET 
  ANSI_NULLS, 
  QUOTED_IDENTIFIER, 
  CONCAT_NULL_YIELDS_NULL, 
  ANSI_WARNINGS, 
  ANSI_PADDING 
ON;

UPDATE ---->

i set the attribute PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION for see if the server show me something and I received this:

Error conectando con la base de datos: SQLSTATE[HY000]: General error: 1934 General SQL Server error: Check messages from the SQL Server [1934] (severity 16) [SELECT top 5 Document from Companies where Document = 'xxxxxxxx']

I checked the error in SQL and I get this:

%ls failed because the following SET options have incorrect settings: '%.*ls'. Verify that SET options are correct for use with %S_MSG..

Anyone have any idea why I can not make queries in views?

Thanks for reading me :)

As i see in code you use "Document" instead of "AccountDocument" So replace AccountDocument with Document.

$sql = "SELECT top 5 Document from Companies where Document = 'xxxxxxxx'";
$stmt = $db->prepare($sql);
$stmt->execute();

if ($data = $stmt->fetch()) {
    do {
        echo $data['Document'] . '<br>';
    } while ($data = $stmt->fetch());
} else {
    echo 'Empty Query';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM