2

So I have the following database table called "relacionproveedorfamilia"

-----------------------------
| idProveedor | idFamilia   | 
-----------------------------
|      5      |      1      |
-----------------------------
|      5      |      2      |
-----------------------------
|      6      |      2      |
-----------------------------  

I use a function where I provide the value for idProveedor and it should return an array with the idFamilia values that belong to the provided idProveedor.

For example, this is the query to get all idFamilia for idProveedor = 5

SELECT idFamilia FROM relacionproveedorfamilia WHERE idProveedor = 5;

The result of the query is:

---------------
| idFamilia   | 
---------------
|      1      |
---------------
|      2      |
---------------

I am trying to get the query result into an array, so I can display the contents in my view.

In my model Proveedormodel I have the following function, which I've coded by checking other similar questions in this site.

public function obtenerIdFamiliaProveedor($idProveedor){
     $query = $this->db->select('idFamilia')->from('relacionproveedorfamilia')->where('idProveedor', $idProveedor);
     $arrayFamilias = array();

     while($row = mysql_fetch_assoc($query)){
         $arrayFamilias[] = $row;
     }

     return $arrayFamilias;
 }

But in my view I am getting the error message:

Message: mysql_fetch_assoc() expects parameter 1 to be resource, object given

How could I fix this?

1
  • I think you worked a lot with core PHP. Please go through the docs of codeigniter and check how active record query works. Commented Mar 31, 2017 at 4:34

2 Answers 2

5

You are mixing codeigniter Query builder class with mysql_ functions. Why are you doing this?

You have to change your code:

while($row = mysql_fetch_assoc($query)){

to this:

foreach ($query->result_array() as $row)

And you need to add get method at the end of your query:

$query = $this->db->select('idFamilia')->from('relacionproveedorfamilia')->where('idProveedor', $idProveedor)->get();
Sign up to request clarification or add additional context in comments.

Comments

0

You do not need to loop to fetch all of the rows from the query in a CodeIgniter application. Just return the result() if you want an array of objects or result_array() if you want an array of arrays.

public function obtenerIdFamiliaProveedor(int $idProveedor): array
{
    return $this->db
        ->select('idFamilia')
        ->get_where(
            'relacionproveedorfamilia',
            ['idProveedor' => $idProveedor]
        )
        ->result_array();  // for an array of objects, use `->result()` instead
}

The above method will return an array of zero-or-more 1-element arrays. In other words, the array may be empty or it may contain a 2d array with a solitary column of data.

For more reading about converting query result objects into arrays/objects, see: Difference between row_array() and result_array()

As a general rule, I strongly recommend never returning false from a model method which is expected to return payload of an array or an object. CodeIgniter methods which return a single-dimension payload will return null when there is no qualifying record found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.