views:

245

answers:

2

Hello,

What I want to do is while executing MySQL query, passing column data into a PHP function and comparing the result with WHERE clause...

Something like that, I have the slug() function which I wrote in PHP and my query will be something like;

SELECT * FROM articles WHERE slug(author) = "james-taylor";

So the query will select "author" column and pass it to slug() function and compare returning data with "james-taylor".

Anybody have any ideas? Thanks in advance :)

A: 

That’s not possible. But you can create a function in MySQL that does the same as your PHP function.

Gumbo
Except you now have two instances of the same code to maintain, which will become a maintenance problem when you need to adjust what slug does.
Sohnee
+2  A: 

There's not a way to use a PHP function directly in mysql.

You would need to use mysql string functions to duplicate your slug function in mysql. (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html)

Another option would be to have a field named "slug" and to use the php function to generate slugs for all of your records in the table. Then you can say "select * from articles where slug = 'james-taylor';

$slug = slug('James Taylor');
$sql = "select * from articles where slug = '{$slug}'";
Dooltaz
Yep. Of course this can be brittle if you ever want to update/change/correct an author's name, which is why most sites use ID+slug in URLs rather than just slug.
bobince
Recommend you use a query parameter instead of interpolating `$slug` into the SQL string.
Bill Karwin
having a field named "slug" is the good and easier option, I'll use that idea, thank you very much :)
she hates me
Unescaped values in SQL....ahhhhhhhh!
Darryl Hein