CodeIgniter 4-从数据库问题中获取结果

发布于 2025-02-10 05:27:56 字数 954 浏览 0 评论 0原文

我是Codeigniter 4的新手,并且有一些问题从数据库中检索数据。

在我的控制器中,我有:

$partno = model(Ecart_model::class)->getPartno($productid);
        echo json_encode($partno);

在我的模型中,我有:

public function getPartno($productid){
        $db = db_connect();
        $partno = $db->table('products')->select('partno')->where('id', $productid)->get();
        return $partno;
    }

在我的ajax函数中,我有:

function addtoecart(ip,productid){
    var postData = {ip: ip, productid: productid};

    $.ajax(
        {
            url : 'https://www.myurl.com/ecart/add',
            type: 'POST',
            data : postData,
            dataType: "json",

            success: function(result) {
                alert(result);
            },
        });
}

功能性方面的一切(这不是问题)。我存在的问题是从数据库中检索到的值/数据,并将其传递回AJAX函数,在成功之后,警报显示:[对象对象],而不是从数据库中检索到的实际值,

我希望这很有意义?您知道代码怎么了吗?

谢谢!

I'm new to codeigniter 4 and having some issues retrieving data from my database.

In my controller, I have:

$partno = model(Ecart_model::class)->getPartno($productid);
        echo json_encode($partno);

In my model, I have:

public function getPartno($productid){
        $db = db_connect();
        $partno = $db->table('products')->select('partno')->where('id', $productid)->get();
        return $partno;
    }

In my Ajax function, I have:

function addtoecart(ip,productid){
    var postData = {ip: ip, productid: productid};

    $.ajax(
        {
            url : 'https://www.myurl.com/ecart/add',
            type: 'POST',
            data : postData,
            dataType: "json",

            success: function(result) {
                alert(result);
            },
        });
}

Everything in terms of functionality works (which isn't the issue). The issue I am having is the value/data that is retrieved from the database and is passed back to the Ajax function, on success, the alert shows: [object Object] instead of the actual value retrieved from the database

I hope that makes sense? Do you know what is wrong with the code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-02-17 05:27:56

这不是答案吗?

public function getPartno($productid){
    $db = db_connect();
    
    $row = $db->table('products')->select('partno')->where('id', $productid)->get()->getRowArray();
    
    return $row['partno'];
}

我也不确定jQuery在AJAX结果上解码JSON数据,因此我也会尝试json.parse(result)。尽管当您返回int时,您可能根本不需要JSON,并且可以简单地输出它。

Is this not the answer?

public function getPartno($productid){
    $db = db_connect();
    
    $row = $db->table('products')->select('partno')->where('id', $productid)->get()->getRowArray();
    
    return $row['partno'];
}

I'm also not sure that Jquery decodes json data on an ajax result so I would also try JSON.parse(result) as well. Although as you're returning an int you probably don't need the JSON at all and could simply output it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文