用于获取数据库数据的 URI (CodeIgniter)

发布于 2024-12-27 03:17:40 字数 630 浏览 2 评论 0原文

所以我尝试利用 CodeIgniter 的 URI 分段功能。我将 URI 段用作 id 以从数据库检索信息。 它工作正常,但我注意到如果我输入数据库中不存在的 id,它会抛出数据库错误。

我该如何防止这种情况发生?

模型代码:

$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

在控制器中调用模型:

$question_id = $this->uri->segment(3);
$data['question'] = $this->forum_model->get_question($question_id);

PHP 错误:

遇到 PHP 错误
严重性:通知
消息:尝试获取非对象的属性

So I'm trying to utilise CodeIgniter's URI segment functionality. I have the URI segments being used as ids to retrieve information from the database.
It's working fine, but I have noticed that if I put in an id that doesn't exist in the database, it spits out a database error.

How do I prevent this from happening?

Model Code:

$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

Call to model in controller:

$question_id = $this->uri->segment(3);
$data['question'] = $this->forum_model->get_question($question_id);

PHP error:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object

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

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

发布评论

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

评论(2

末骤雨初歇 2025-01-03 03:17:40

在调用 $query->row 之前,您需要检查查询是否返回任何结果。

方法是:

$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
    if($query->num_rows() > 0)
          return $query->row();
    else
          return false;

$query->num_rows() 将为您提供返回的行数。

You need to check if the query returned any results before calling $query->row

The way to do it is:

$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
    if($query->num_rows() > 0)
          return $query->row();
    else
          return false;

$query->num_rows() will give you the number of rows returned.

灯角 2025-01-03 03:17:40

要调试,只需执行此操作...

    //if this gives u no result, then thats the problem because it couldnt find the 
    //$this->uri->segment(3);
$question_id = $this->uri->segment(3);
echo $question_id; 

//if this prints out the expected result.
//then you should know that the problem is still somewhere down the code.
$data['question'] = $this->forum_model->get_question($question_id);
var_dump($data['question']);


//this is for your model.
echo $question_id
$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

注意:如果所有这些回显都输出有效值,则检查其他地方是否存在问题

to debug just do this...

    //if this gives u no result, then thats the problem because it couldnt find the 
    //$this->uri->segment(3);
$question_id = $this->uri->segment(3);
echo $question_id; 

//if this prints out the expected result.
//then you should know that the problem is still somewhere down the code.
$data['question'] = $this->forum_model->get_question($question_id);
var_dump($data['question']);


//this is for your model.
echo $question_id
$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

NOTE: if all this echos outputs valid values, then check somewhere else for the problem

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