1

Within a MySQL query, is it possible to put a PHP function around a field name in order to change/replace its value?

SELECT * FROM my_table WHERE url_title(field_name) = '$variable'

The reason for asking is that the '$variable' in my URL which the query will use is an edited version (using Codeigniter's url_title function) of its true value eg. $variable = 'dangelo' whereas its true value in field name is "D'Angelo".

3
  • 1
    It sounds like url_title() is making a URL slug - once it is changed to "dangelo" it cannot be reversed to "D'Angelo" - it is normal to store these values separately. One for the URL and the other for the page title.
    – halfer
    Mar 28, 2015 at 0:20
  • Using WHERE some_complicated_function(column) = 'some value' in your query will hurt performance, especially when you have thousands of articles. You should consider halfer's suggestion.
    – Salman A
    Mar 28, 2015 at 21:36
  • How about this...you get the result first and then execute the query. $var = url_title(field_name) and the query would be SELECT * FROM my_table WHERE $var = '$variable' Mar 28, 2015 at 21:52

2 Answers 2

0

You can implement the url_title function as a MySQL stored procedure, which would require translating the PHP code of that function to a MySQL stored procedure.

Note that the performance would not be great due to having to call this function for every row on each query.

The more typical solution is to have another field for the slug, pre-generate the values for each article's slug. On the query, you would compare $variable to the new field.

2
  • Care to be a bit more specific? Mar 28, 2015 at 21:32
  • Seems like field_name confused me a bit. I've fixed my solution. Mar 28, 2015 at 21:50
0

Yes it's possible. But not at all recommended because it wouldn't be able to make use of the index. Create another column with the actual value you want to find and add an index to that column.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.