【问题标题】:Correct sql query in CodeIgniter is giving an errorCodeIgniter 中的正确 sql 查询给出错误
【发布时间】:2019-08-29 06:31:35
【问题描述】:

当我从 CodeIgniter 运行查询时,我收到此错误。

    A Database Error Occurred
    Error Number: 42000/263
    [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Must specify 
    table to select from.
    SELECT *
    Filename: D:/xampp/htdocs/4hifi/system/database/DB_driver.php

这令人困惑,因为直接在 SQL-Server 中执行的完全相同的查询给出了正确的结果。

我正在使用 CodeIgniter 3.1.9 ,我已经尝试注入 $username 变量以不同方式查询,都给出相同的错误。

代码如下:

$sql = "select date, g1.product_name, g2.order_amount, g1.price, g1.id, g1.order_id, g1.action from dbo.orders g1 inner join (select product_name, SUM( order_amount) as order_amount from dbo.orders where action=1 and confirmed!=1 group by product_name) g2 on g2.product_name = g1.product_name where g1.confirmed !=1 and g1.kontrahent = ? and action = 1";

        $db2->query($sql, $username);


        $result = $db2->get()->result_array();

        return $result;

【问题讨论】:

    标签: php sql-server codeigniter codeigniter-3


    【解决方案1】:

    $db2->query($sql, $username); 行本身应该返回所需的结果。如果是原始查询,则无需执行db->get()

    【讨论】:

    • 是的,我根据您的回答对代码进行了更改,现在正在运行,非常感谢;) $query = $db2->query($sql, $username); $result = $query->result_array();
    【解决方案2】:

    为什么要分两步做。你应该使用这样的东西

    $sql = "select date, g1.product_name, g2.order_amount, g1.price, g1.id, g1.order_id, g1.action from dbo.orders g1 inner join (select product_name, SUM( order_amount) as order_amount from dbo.orders where action=1 and confirmed!=1 group by product_name) g2 on g2.product_name = g1.product_name where g1.confirmed !=1 and g1.kontrahent = ? and action = 1";
    
        $result = $sql->result_array();
    
        return $result;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      相关资源
      最近更新 更多