bigjdve
asked on
How to bind variables using mysqli for unknown number of field
My prepare statement: "select * from tblname";
How do I bind the number of unknown field being retrieved?
It could be 2 or 10.
Help!
I know how to bind if I know that it is going to return 2 fields (see code below).
How do I bind it if I don't know how many fields are being retrieved so that I can bind the result and then fetch all the values???
if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
$sql->execute();
/* bind variables to prepared statement */
$sql->bind_result($col1, $col2);
/* fetch values */
while ($sql->fetch()) {
printf($col1." and ".$col2."<br />");
}
/* close statement */
$sql->close();
}
How do I bind the number of unknown field being retrieved?
It could be 2 or 10.
Help!
I know how to bind if I know that it is going to return 2 fields (see code below).
How do I bind it if I don't know how many fields are being retrieved so that I can bind the result and then fetch all the values???
if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
$sql->execute();
/* bind variables to prepared statement */
$sql->bind_result($col1, $col2);
/* fetch values */
while ($sql->fetch()) {
printf($col1." and ".$col2."<br />");
}
/* close statement */
$sql->close();
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
darkyin887 -- again, thank you. I actually figure this out already. But thank you. However, I am now stuck on how to retrieve my data. I used a while loop to go through my resultset to get my data. This table only have 2 fields (date, value). To retrieve the date field, I need to specified that exact field (i.e. $result['date']). Is there a way to retrieve this field without specifying the field name.
See my code:
if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
$sql->execute();
$sql->store_result();
$meta = $sql->result_metadata();
$bindVarsArray = array();
while ($column = $meta->fetch_field()) {
$bindVarsArray[] = &$results[$column->name];
}
call_user_func_array(array ($sql, 'bind_result'), $bindVarsArray);
$sql->fetch();
while ($sql->fetch())
{
echo "here= ".$results['value']." <br>";
}
$sql->close();
}
See my code:
if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
$sql->execute();
$sql->store_result();
$meta = $sql->result_metadata();
$bindVarsArray = array();
while ($column = $meta->fetch_field()) {
$bindVarsArray[] = &$results[$column->name];
}
call_user_func_array(array
$sql->fetch();
while ($sql->fetch())
{
echo "here= ".$results['value']." <br>";
}
$sql->close();
}
I am sorry.. I think that is not possible.
ASKER
solution was resolved with the call_user_func
ASKER
Do I have to have 10 bind variables ($sql->bind_result($col1, $col2...$col10)??