简体   繁体   English

CodeIgniter中的正确SQL查询给出了错误

[英]Correct sql query in CodeIgniter is giving an error

When I am running a query from CodeIgniter, I am getting this error. 当我从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

Which is confusing cause exactly the same query executed directly in SQL-Server is giving correct results. 这令人困惑,因为直接在SQL Server中执行的完全相同的查询给出了正确的结果。

I am using CodeIgniter 3.1.9 , I already tried to inject $username variable to query in different ways, all are giving the same error. 我正在使用CodeIgniter 3.1.9,我已经尝试注入$ username变量以不同的方式进行查询,所有操作都给出相同的错误。

Here is the code: 这是代码:

$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;

The $db2->query($sql, $username); $db2->query($sql, $username); line itself should return the required result.No need to do db->get() in case of raw queries. 行本身应该返回所需的结果。如果是原始查询,则无需执行db->get()

why are you doing that in two steps. 为什么要分两个步骤进行 You should use something like this 你应该用这样的东西

$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;

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

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