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?
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();