Variable amount of columns returned in mysqli prepared statement

Posted by manyxcxi on Stack Overflow See other posts from Stack Overflow or by manyxcxi
Published on 2010-05-27T19:39:22Z Indexed on 2010/05/27 19:41 UTC
Read the original article Hit count: 268

Filed under:
|
|

I have a situation where a dynamic query is being generated that could select anywhere from 1 to over 300 different columns across multiple tables. It currently works fine just doing a query, however the issue I'm running into in using a prepared statement is that I do not know how to handle the fact that I don't know how many columns I will be asking for each time and therefor don't know how to process the results. The reason I believe a bind statement will help is because once this query is run once, it will most likely (though not always) be run again with the exact same parameters.

Currently I have something like this:

$rows = array();
$this->statement = $this->db->prepare($query);
$this->statement->bind_param('i',$id);
$this->statement->execute();
$this->statement->bind_result($result);
while($this->statement->fetch())
{
   $rows[] = $result;
}

I know this doesn't work as I want it to, my question is how do I get the data back out of the query. Is it possible to bring the columns back in an associative array by column name, like a standard mysqli query?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysqli