Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
		}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

This should get you started:

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

This will 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.