0

I have a very complex PHP function which would take a lot of time to rewrite in SQL. So I was wondering if it is possible in anyway to do something like that.

For example:

$query = "SELECT p.id FROM pages p WHERE check_access(p.id) = 1"
mysql_query($query);
...

check_access would be the PHP function.

4
  • 6
    No, sql and php are evaluated in different contexts this is not valid. Commented Jun 11, 2013 at 19:27
  • 2
    Have you considered at least trying to re-write the PHP function in SQL? If you're having trouble, you can always post a question on Stack Overflow, if you've put some effort into it. Then again, if it isn't worth your time, just SELECT all the data then filter it in PHP. Commented Jun 11, 2013 at 19:30
  • RTLM? dev.mysql.com/doc/refman/5.1/en/adding-udf.html Commented Jun 11, 2013 at 19:31
  • This is quite like asking whether the people on TV can hear you if you shout at the screen :) In this analogy the TV is PHP, and the broadcaster is the DB Commented Jun 11, 2013 at 20:00

1 Answer 1

2

It is not possible since, SQL interpretion is done separately from PHP interpretion.

You can only use the output of a php function inside an SQL query.

enter image description here

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

5 Comments

As a slight addition to this diagram, it's entirely possible (though not advisable) for the PHP server to be in Japan while the DB it connects to is in Belgium etc - there's no requirement for them to be on a single server or even on the same continent.
@Simonatmso.net agreed. But, why is it not advisable anyway?
If the database is the opposite side of the world to your PHP server then the latency will be NASTY :) Instead of response times of <1ms (assuming same physical server / rack / local network) you would see response times in the order of 200-600ms before you even do any query processing.
Do they follow this technique in any web application?
Most likely not, though in a lesser extent you can put your web servers on a cloud service while having your database within a data-centre which would have a less exaggerated replica of the issue, though in that case if done well the latency would be far more tolerable making it a viable option. Anyway best leave this at that, as this is digressing into a tangent unrelated to the original question :)

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.