0

I wonder if the following codes do the same thing?

  1. if ($query->num_rows() == 1) {      
        $row = $query->row_array(); 
        $path = $row['path'];
        $photo = $row['photo'];
    }
    
  2. if ($query->num_rows() == 1) {           
        foreach ($query->result() as $row) {
            $path = $row->path;
            $photo = $row->photo;
        } 
    }
    
1
  • I've been maintaining a large/old CodeIgniter application for more than 6 years and I can't think of one place in the entire codebase where I've actually needed num_rows(). In every possible scenario, there is a cleaner approach that doesn't need it. In your case, if you definitely need to unpack the two columns as two separate variables, then I'd probably recommend $row = $query->row_array(); [$path, $photo] = $row ?? [null, null]; It is easy to identify the row_array() return data as an array or null. Commented Oct 17 at 4:27

1 Answer 1

2

Yes. One gets the result as an object the other an associative array. Same result.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.