Is it possible to perform a specific PHP function on data that is being returned by a database query, at the very moment this query is still running?
Let's say we are in some class and we have the following pseudo SQL which should return a couple of rows:
$results = $this->db->query("SELECT PHP_function(column), column 2 FROM table")->fetchAll(PDO::FETCH_ASSOC);
PHP_function could be json_decode for example.
Or is this not possible and would it require an additional loop on the results in order to do such a thing?
4 Answers 4
A manual page for the very function you're using contains an example
2 Comments
func_get_args is natural behaviour. I would instead rather have one argument passed to the function of FETCH_FUNC, which would be an indexed array of the columns returned from the database query.No, this is not possible. SQL engine (mysql or any other) is a separate application. PHP can communicate with it, send queries and receive data. Depending on what you want to do in that custom function, you may be able to write a custom function in mysql to do the same thing.
Have a look at create function documentation for info on how to do that.
4 Comments
($row = mysql_fetch_assoc($query)) after the query, so I hoped there was some way to access this loop. I guess it's in the PDO fetchAll method. Edit: hadn't seen the answer of YCS, but StackoverFlow is now warning me to not accept anything within the next six minutes..Where's the problem with this?
<?php
foreach($results as $key=>$res){
$results[$key]['column'] = PHP_function($res['column']);
}
Beside that MySQL has some useful functions included already, so maybe that one you need is usable right in the query
Comments
As i know it's not possible to achieve php manipulation while query running , you need to take result and then loop aur use predefined function from
return php_function($row['column']);or even not returning, but using it somewhere inside the function