查询生成器和条件语句

发布于 2024-11-11 15:35:10 字数 512 浏览 4 评论 0原文

$query = DB::select('thing')->from('things')->where('thing', '=', 'something');

if ($other_thing)
{
    $query->and_where('other_thing', '=', 'something else');
}

$query->order_by('thing', 'ASC')->limit(10)->execute()->as_array();

foreach ($query as $row)
{
    echo $row['thing'];
}

问题是什么?

嗯:

echo $row['thing'] -> nothing.
print_r($query) -> an object and not an array.

我做错了什么?有人可以帮我解决这个问题吗?请!

谢谢你!

$query = DB::select('thing')->from('things')->where('thing', '=', 'something');

if ($other_thing)
{
    $query->and_where('other_thing', '=', 'something else');
}

$query->order_by('thing', 'ASC')->limit(10)->execute()->as_array();

foreach ($query as $row)
{
    echo $row['thing'];
}

And what the problem is?

Well:

echo $row['thing'] -> nothing.
print_r($query) -> an object and not an array.

What am I doing wrong? Can someone help me on this? Please!

Thank you!

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

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

发布评论

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

评论(2

赠我空喜 2024-11-18 15:35:10

试试这个:

$result = $query->order_by('thing', 'ASC')->limit(10)->execute()->as_array();

foreach ($result as $row)
{
    echo $row['thing'];
}

try this:

$result = $query->order_by('thing', 'ASC')->limit(10)->execute()->as_array();

foreach ($result as $row)
{
    echo $row['thing'];
}
夕嗳→ 2024-11-18 15:35:10

To expand on the above answer, the problem is that the execute function is actually returning an instance of Database_Result. As the above post points out you are able to call various functions on this object that will then return the data in various formats (see the previous link for a full list of available functions)

This provides various benefits - this page describes them all

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