简体   繁体   English

使用PDO(dblib)在PHP中进行SELECT到SQL视图

[英]SELECT in Php with PDO (dblib) to SQL view

I am trying to execute a SQL query and it does not work. 我正在尝试执行一个SQL查询,它不起作用。

$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. 但事实并非如此,因为如果我在SQL中执行该查询,它将运行完美。

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: 我设置属性PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION以查看服务器是否向我显示了一些内容,并且我收到了以下内容:

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'] 错误的基本连接信息:SQLSTATE [HY000]:常规错误:1934常规SQL Server错误:检查来自SQL Server的消息[1934](严重性16)[从文档='xxxxxxxx'的公司中选择前5个文档]

I checked the error in SQL and I get this: 我检查了SQL中的错误,我得到了:

%ls failed because the following SET options have incorrect settings: '%.*ls'. %ls失败,因为以下SET选项设置不正确:'%。* ls'。 Verify that SET options are correct for use with %S_MSG.. 验证SET选项正确用于%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. 正如我在代码中看到的那样,您使用“文档”而不是“ AccountDocument”,因此将AccountDocument替换为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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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