MySQL select as variable to be used in a function defined in PHP <p>I think this a pretty classical setting: Suppose I have a <code>table tab1</code>, with <code>columns c1, c2</code>. Then I want to <code>select c1, c2</code> as variable of a function <code>fun(a,b)</code> in another select:</p> <pre><code>SELECT fun(@a,@b) as r FROM (SELECT @a:=c1, @b:=c2 FROM tab1) AS tab ORDER BY r LIMIT 10; </code></pre> <p>The fun is pre-defined in PHP:</p> <pre><code>function fun($d, $t){ $timenow = time(); $perd = 45000; $di = (int)$d; $tf = (float)$t; if ($di === 0) { return 0; }else{ return (Log($di)/Log(10)+($timenow-$tf)/$perd); } } </code></pre> <p>The problem is that the variable is not updated at all. I build a test environment in <a href="http://sqlfiddle.com/#!9/9ffd6" rel="nofollow">http://sqlfiddle.com/#!9/9ffd6</a>, the <code>fun(@a,@b)</code> is replaced by <code>@a</code> for simplicity.</p> <hr> <p>UPDATE</p> <p>It seems that my original question canot properly describe my problem. And I updated it, thanks to @wajeeh, the solution could be:</p> <ol> <li>translate my php function into MYSQL form (it is a litter hard for me)</li> <li>Use php function, but return the mysql results as array. (In that case I need to write the odering function by hand! and then (maybe) need another SELECT of MYSQL?)</li> </ol>