I want to do a query in my model from a table known as jurisdictions. Now I want this query to provide a valid MySQL result resource. I.e I want to pass the result to mysql_fetch_array()
. If I pass in $query = $this->db->query()
. I get an error saying that the passed in argument is invalid. I was wondering how can I convert $query
to a MySQL result recource.
If I'm understanding you correctly then why not just use the available methods of:
$query->result();
or
$query->result_array();
Choose whichever to suit your needs.
Well, if you want to have a MySQL resource, you should be using mysql_query
.
Codeigniter already has a method which will give you one row at a time: row_array
(actually, it has two, the other is just row
, but that returns an object, not an array). If you want to get numeric indexes on the result of result_array, use array_values:
$result = $this->db->query( "SELECT 'foo' as foo_rules FROM DUAL" );
$aso_arr = $result->row_array(); // assoc. array w/o numeric indexes
echo $aso_arr[ 'foo_rules' ];
$num_arr = array_values( $aso_arr );
echo $num_arr[ 0 ];
If you would like the entire result of the selection, then use result
and result_array
(they have behavior similar to row and row_array, only they return the whole result set in an array)
I repeat my first sentence, but you can get the MySQL resource this way:
$result = $this->db->query( "SELECT 'foo' as foo_rules FROM DUAL" );
$resource = $result->result_id;
But, since this is not documented, it should not be considered supported or even expected behavior. Be forewarned.