3

I'm working on a MySQL/php project, and I came across the situation where I was getting the results of a query from MySQL and dumping them into a HTML table. Now I am wrapping the cells of a particular column in the table in some anchor tags, and I was just wondering was it better or more efficient to do the wrapping in php or in the MySQL Query itself, e.g.:

...
$row_results_from_query['user'] = '<a href="?user=' . $row_results_from_query['user'] . '">' . $row_results_from_query['user'] . '</a>';
...

OR use the query

SELECT ..., CONCAT("<a href='?user=", Users.user, "'>", Users.user, "</a>") FROM ...

I figured that the php way was faster, however I found some articles which suggested that for some things MySQL is faster.

What is best practice? Why?

Or, is it better to create a stored procedure in MySQL instead?

1
  • As long you can read your SQL-Query afterwards everything is fine... :-) Commented Aug 21, 2012 at 9:38

1 Answer 1

3

"is it better to create a stored procedure in MySQL instead?" No, as this is typical use case for templates (see: http://en.wikipedia.org/wiki/Model–view–controller). PHP provides a lot more options to parse templates, than MySQL does. Just try to keep in mind any possible future changes, e.g. switching the URL-format from ?user=XXX to ?uid=XXX, or something similar. In MySQL you need to remove an re-create the stored procedure or you need to work on the queries each time, which is error-prone. If you use some kind of a template for such a task, you just need to edit that.

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

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.