0

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?

5
  • Why would you want to do that at query time, and not later on? Commented May 21, 2013 at 13:04
  • Why do you need it as the query is running? What's wrong with looping through the data afterwards? Commented May 21, 2013 at 13:05
  • If you need functionality the RDBMS doesn't already provide (it provides a lot), you may need a user defined function Commented May 21, 2013 at 13:05
  • It's an additional loop. Commented May 21, 2013 at 13:05
  • 1
    for me seems more legit: return php_function($row['column']); or even not returning, but using it somewhere inside the function Commented May 21, 2013 at 13:06

4 Answers 4

2

A manual page for the very function you're using contains an example

Sign up to request clarification or add additional context in comments.

2 Comments

Any suggestions as to how I can force the arguments of PDO::FETCH_FUNC to be associative? PDO::FETCH_ASSOC does not achieve this. This is of course because arguments can't really be associative, thus receiving a numerically indexed array from 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.
I wouldn't waste time with research but rather use fetch() in a while loop instead of fetchAll() and apply whatever complex logic in this loop.
0

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

Before PDO I used to be able to perform any function in the while loop ($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..
@user2180613 This is not the same as you're asking. You can still do the loop, but you cannot use php function in the select clause.
I know it is not possible to mix the functions of two languages. See my question's first sentence.
@user2180613 You asked whether it's possible to execute a function on a query when it's still running - and the answer is "no". When you're retrieving the data in a loop, the query has completed, it's not running any more.
0

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.