gpt4 book ai didi

php - yii mssql 查询不工作

转载 作者:行者123 更新时间:2023-11-29 13:57:59 26 4
gpt4 key购买 nike

我有以下查询:

Yii::app()->dbNav->createCommand()
->select('*')
->from('dbo Spectrum Geo Limited$Purch_ Inv_ Header')
->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
->queryRow();

它失败并出现以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Invalid object name 'dbo'. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [dbo] [Spectrum Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = :id

我的 dblib 连接适用于 mssql。我正在尝试将以下内容转换为 yii

$sql="SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = $q ";

在 Management studio 中,我运行以下命令以获得所需的结果:

SELECT TOP 1000 *
FROM [Navision].[dbo].[Spectrum Geo Limited$Purch_ Inv_ Header]
where [Log Number] = 4593

以下方法可行,但这是理想的方法吗?

Yii::app()->dbNav->createCommand("SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = ".$_GET['lognumber'] );

进一步

如果我执行以下操作

Yii::app()->dbNav->createCommand()
->select('*')
->from('[dbo].[Spectrum Geo Limited$Purch_ Inv_ Header]')
->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
->queryRow();

我收到以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = '4593''. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[dbo]].[[Spectrum] [Geo Limited$Purch_ Inv_ Header]]
WHERE "Log Number" = :id

现在我已经尝试过:

Yii::app()->dbNav->createCommand()
->select('*')
->from('[Spectrum Geo Limited$Purch_ Inv_ Header]')
->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
->queryRow();

错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = '4593''. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]]
WHERE "Log Number" = :id

删除 where 子句会出现此错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]'. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]]

最佳答案

您需要将表名称括起来,因为表名称中包含空格。

通常这被认为是糟糕的设计,但看起来这是来自第三方供应商的数据库,因此您可能无法更改它。

编辑:您可能也不需要 [dbo] ...

编辑 2:现在导致问题的是 where 子句中的列名称中的空格。

编辑 3:如果您查看有效的原始查询:

Yii::app()->dbNav->createCommand("SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = ".$_GET['lognumber'] );

然后将其与此进行比较:

SELECT * FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]] 

您会发现括号不同。

使用它应该可以工作:

SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header]

编辑4:

好的,如果您使用以下内容会发生什么?

Yii::app()->dbNav->createCommand()
->select('*')
->from('Spectrum Geo Limited$Purch_ Inv_ Header')
->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
->queryRow();

我省略了 dbo. 部分,因为您的 SQL 语句之前在没有它的情况下也可以工作。通过保留所有空格,我们可能会激活之前让你头疼的 Yii 自动包围曝光...

关于php - yii mssql 查询不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15523897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com