tags:

views:

379

answers:

1

Below is my code for the function I am using to retrieve multiple data from my table, but I would like to use bind_result($array[0],...,..) to be generated automatically depending on number of fields i am selecting in the query.

for example..

$query=select a,b,c,d,e from table;//selecting 5 fields
......
$stmt->execute();$stmt->bind_result($retrieve[0],$retrieve[1],$retrieve[2],$retrieve[3],$retrieve[4]);

(the bind_result for 5 values should be automatically generated) Help will be appreciated ...Thanks

$query="SELECT comment, userid,UNIX_TIMESTAMP(dtime)
    FROM comment_updates
    WHERE updateid=46546
    ORDER BY dtime DESC
    LIMIT 10 ";
  if($stmt = $this->conn->prepare($query)) {
   $stmt->execute();
   $stmt->bind_result($comments[0],$comments[1],$comments[2]);
   $i=0;
   while($stmt->fetch()){
   $i++;
   $name='t'.$i;
   $$name = array($comments[0],$comments[1],$comments[2]);
   }
   return array($i,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10);
   $stmt->close();
  }
+1  A: 

This should get you started:

http://uk2.php.net/manual/en/mysqli-stmt.result-metadata.php

This'll get you the number of fields in your resultset via mysqli_num_fields().

This should be the size of your $retrieve array.

As bind_result doesn't take an array as an argument, you'll need to use call_user_func_array to achieve this:

call_user_func_array(array($stmt, 'bind_result'), $retrieve_references);

$retrieve_references should be an array of references to the elements in $retrieve. Using $retrieve itself in call_user_func_array will trigger an error.

jetboy