gpt4 book ai didi

php - 如何正确检索 num_rows |代码点火器

转载 作者:行者123 更新时间:2023-11-29 05:37:20 25 4
gpt4 key购买 nike

是否有更好的方法从表中检索行数?目前我有这个如果没有结果也会返回错误:

错误: 遇到 PHP 错误

<p>Severity: Notice</p>
<p>Message: Undefined property: Home::$count</p>
<p>Filename: core/Model.php</p>
<p>Line Number: 51</p>

</div>{"status":"ok","content":""}

Controller

 public function do_messages()
{
$this->load->model('message_model');
$user_id = $this->input->post('user_id');
$this->message_model->unread_msg($user_id);
$mess = $this->message_model->unread_msg_count();
if(isset($mess)){
$result = array('status' => 'ok', 'content' => $mess);
echo json_encode($result);
exit();
}else{
$result = array('status' => 'ok', 'content' => '');
echo json_encode($result);
exit();
}
}

型号:

function unread_msg($user_id)
{
$query_str = "SELECT id FROM messages WHERE view = 0 AND dest = ?";
$query = $this->db->query($query_str, $user_id);

if($query->num_rows() > 0)
{
$this->count = $query->num_rows();
}
}
function unread_msg_count()
{
return $this->count;
}

js文件:

function get_messages()
{
$.post(base_url + "index.php/home/do_messages", { user_id : user_msg }, function(data) {
if (data.status == 'ok')
{
$(".new_msg").removeClass('hide').html(data.content);
}
else
{
}
}, "json");
}
get_messages();

最佳答案

试试这个:

public function do_messages()
{
$this->load->model('message_model');
$user_id = $this->input->post('user_id');
$this->message_model->unread_msg($user_id);
$mess = $this->message_model->unread_msg_count();
if($mess != false){
$result = array('status' => 'ok', 'content' => $mess);
echo json_encode($result);
exit();
}else{
$result = array('status' => 'ok', 'content' => '');
echo json_encode($result);
exit();
}
}

型号:

function unread_msg($user_id)
{
$query_str = "SELECT id FROM messages WHERE view = 0 AND dest = ?";
$query = $this->db->query($query_str, $user_id);

if($query->num_rows() > 0)
{
return $query->num_rows();
} else {
return false;
}
}
function unread_msg_count()
{
return $this->count;
}

关于php - 如何正确检索 num_rows |代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com