views:

533

answers:

2

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

A: 

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.

Kalium
Thanks Kalium, you said "pushing the resulting associative array onto your result set"... can you provide some sample code of pushing an assoc array onto a result set. So I would use mysqli_result::fetch_assoc() to get each row? then perform function on row? how do I push row back onto result set?
undefined
Can I do this using mysql - I have MySQL version 5.0.22. mysqli commands are returning errors - eg "Call to undefined function mysqli_connect()"
undefined
That's because you apparently don't have the mysqli extension installed. You can push the associative array into your result set like this: $results[]= $associativeArray;
Kalium
Hi thanks, I tried the following - $result = mysql_query($sql); $result = mysql_fetch_assoc($result); $arraySize = count($result);for ($i=0; $i<$arraySize; $i++) { $result[i]['file_name'] = "TESTING";}; return $result;No joy, AMFPHP output the first row only and file_name had orig value.
undefined
+1  A: 

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.

KOGI