Question

The following code from http://php.morva.net/manual/en/mysqli-stmt.bind-result.php shows a mysqli query being prepared and executed. while ($stmt->fetch()) loop looks like it is generating the result resource. Can I change this to include a call to a function e.g.

while ($stmt->fetch()) {
       foreach($row as $key => $val)
       {
           $c[$key] = performFunction($val);
       }
       $result[] = $c;
   }

Then instead of print_r($result) I would return($result). That way I can dynamically change the value of $val
The original code =

if ($stmt = $mysqli->prepare("SELECT * FROM sample WHERE t2 LIKE ?")) {
   $tt2 = '%';
     $stmt->bind_param("s", $tt2);
   $stmt->execute();

   $meta = $stmt->result_metadata();
   while ($field = $meta->fetch_field())
   {
       $params[] = &$row[$field->name];
   }

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

   while ($stmt->fetch()) {
       foreach($row as $key => $val)
       {
           $c[$key] = $val;
       }
       $result[] = $c;
   }
     $stmt->close();
}
$mysqli->close();
print_r($result);

Would this work, how else could I do this?
Thanks all...

Was it helpful?

Solution

You could use a UDF (User Defined Function) to process the data on the MySQL side of things before it ever makes it back to PHP.

OTHER TIPS

That's a perfectly valid method, although I suspect your sample implementation code won't quite work. You're probably better off fetching a mysqli_result object, calling fetch_assoc on it, and pushing the resulting associative array onto your result set rather making the associative array yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top