Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

java - Message: count(): Parameter must be an array or an object that implements Countable codeigniter on centos

see my query

Select * from join_chat where
(user_1 = '24' and user_2 = '26') or
(user_1 = '26' and user_2 = '24')

please advice with a solution to this error from my api

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>


</div>{"responce":true,"data":"20"}

2020-05-09 23:16:43.302 28439-30249/techline.carsapp W/System.err: org.json.JSONException: Value Select of type java.lang.String cannot be converted to JSONObject 2020-05-09 23:16:43.302 28439-30249/techline.carsapp W/System.err: at org.json.JSON.typeMismatch(JSON.java:112) 2020-05-09 23:16:43.302 28439-30249/techline.carsapp W/System.err: at org.json.JSONObject.(JSONObject.java:163) 2020-05-09 23:16:43.302 28439-30249/techline.carsapp W/System.err: at org.json.JSONObject.(JSONObject.java:176) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at util.CommonAsyTask.doInBackground(CommonAsyTask.java:104) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at util.CommonAsyTask.doInBackground(CommonAsyTask.java:23) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2020-05-09 23:16:43.303 28439-30249/techline.carsapp W/System.err: at java.lang.Thread.run(Thread.java:764)

Message: count(): Parameter must be an array or an object that implements Countable

Filename: controllers/Api.php

Line Number: 801

    <p>Backtrace:</p>

The method in the controller below

public function send_chat_data()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('join_id', 'join_id', 'trim|required');
    $this->form_validation->set_rules('sender_id', 'sender_id', 'trim|required');
    $this->form_validation->set_rules('message', 'message', 'trim|required');

    if ($this->form_validation->run() == false) {
        $data["responce"] = false;
        $data["error"] = 'join id is required';
    } else {
        $post_date=date("Y-m-d m:i:s");
        $send_chat = array(
                               "join_id"=>$this->input->post("join_id"),
                               "sender_id"=>$this->input->post("sender_id"),
                               "message"=>$this->input->post("message"),
                               "created_date"=>$post_date
                               );

        $this->db->insert("chat", $send_chat);
        $insertid = $this->db->insert_id();

        $q = $this->db->query("Select * from join_chat where join_id= '".$this->input->post("join_id")."' limit 1");
        $recever_id = 0;
        $row = $q->row();
        if ($row->user_1 == $this->input->post("sender_id")) {
            $recever_id = $row->user_2;
        } else {
            $recever_id = $row->user_1;
        }
        $q2 = $this->db->query("Select * from chat where chat_id= '".$insertid."' limit 1");


        $chat = $q2->row();
        $data["responce"] = true;
        $data["data"] = $chat;



        $q_fcm = $this->db->query("Select * from users where user_id= '".$recever_id."' limit 1");
        $row_fcm = $q_fcm->row();

        $registatoin_ids =$row_fcm->user_gcm_code;


        $message["title"] = $this->config->item('app_name');
        $message["message"] = $this->input->post("message");
        $message["image"] = "";
        $message["created_at"] = date("Y-m-d h:i:s");
        $message["obj"] = $chat;

        $this->load->helper('gcm_helper');
        $gcm = new GCM();
        //$result = $gcm->send_topics("/topics/rabbitapp",$message ,"ios");

        $result = $gcm->send_notification(array($registatoin_ids), $message, "android");
    }
    echo json_encode($data);
}

The error is from the line below

$row = $q->row();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem in your question is as I see is that you're trying to count a non-array or non-countable object.

Thoughts

  1. In your entire code you haven't written the count() which seems to be causing the problem, why?
  2. Why do you need to count $q->row(); when you're limiting the result to 1 yourself // as you said the problem is here $row = $q->row();

Possible Solution

When you use row() ie $q->row(); you get a non-countable object,

// row() dummy data
stdClass Object
(
    [id] => 15
    [event_id] => 3
    [event_image] => c1fa8a5d5505047251fd928aa312b16c.jpg
)

but when you use result() ie $q->result();, it will result in an array of objects or in the case of result_array(), an array of arrays, even if you limit them to one.

// result() dummy data -- same as result_object()
(
    [0] => stdClass Object
        (
            [id] => 15
            [event_id] => 3
            [event_image] => c1fa8a5d5505047251fd928aa312b16c.jpg
        )

)

// result_array() dummy data
Array
(
    [0] => Array
        (
            [id] => 15
            [event_id] => 3
            [event_image] => c1fa8a5d5505047251fd928aa312b16c.jpg
        )

)

Both of them are now an array and hence countable.

So, if you must count, use $q->result();
Also, if you want to count the number of "results" $q->row(); has, you can use
count((array) ($q->row()));
which will return 3 here. {id}, {event_id}, {event_image}. Read more about it here

Hope it helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...