Search code examples
databasecodeigniterloadingpyrocms

codeigniter / database page not loading


I have a problem whith a database request : the webpage does not load at all.

    public function get_mylist($user_id){
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('my', '1');
        $this->db->where('user1', $user_id);
        $this->db->or_where('user2', $user_id);
        $query = $this->db->get()->result_array();
        if($query!=NULL)
        {
            foreach($query as $row)
            {
            echo $row['user1'];
                if($row['user2'] != $user_id)
                {
                    $data[] = $row['user2'];
                }
                else if($row['user1'] != $user_id)
                {
                    $data[] = $row['user1'];
                }
            }
            return $data;
        }
        else return 0;
}   

Do you have any idea ?

edit : in my log i have Fatal error: Allowed memory size bytes exhausted

So my loop must be wrong :/


Solution

  • You need to consider how many rows in your DB table and how many bytes per row.

    Do you have enough data in your table to reach the memory limit for your PHP configuration?

    Also, check your logic in your SQL construction.

    Specifically:

     $this->db->where('my', '1');
     $this->db->where('user1', $user_id);
     $this->db->or_where('user2', $user_id);
    

    This where clause is being interpreted as:

    WHERE (`my` = 1 AND `user1` = $user_id) OR (`user2` = $user_id)
    

    Is this what you intended, or do you need:

    WHERE `my` = 1 AND (`user1` = $user_id OR `user2` = $user_id)
    

    In MySQL, OR gets precedence over AND.

    Finally, as a test, take out the code that parses the $data array and return the $row array and print it out using print_r($row) and make sure your query is pulling out the data that you expect.

    Hopefully these ideas will help you diagnose the problem. Best of luck!

    Note: To implement the alternative logic in SQL, you may do something like:

     $this->db->where('my', '1');
     $this->db->where("(`user1` = $user_id OR `user2` = $user_id)");
    

    Pay attention to the opening/closing parentheses immediately to the inside of the double quotes.