28379

MySQL Query with PHP function compare?

Question:

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 :)

Answer1:

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. (<a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/string-functions.html</a>)

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}'";

Answer2:

That’s not possible. But you can <a href="http://dev.mysql.com/doc/refman/5.0/en/create-function-udf.html" rel="nofollow">create a function in MySQL</a> that does the same as your PHP function.

Recommend

  • AttributeError: 'module' object has no attribute 'to_rgb'
  • Change the public folder of Laravel 5.1
  • Show virtual Keyboard when bluetooth keyboard connected?
  • How to implement factorial function into code?
  • AppleScript access Network Folder
  • Program doesn't stop after exception
  • SQL query comparing an attribute in multiple tuples based on values of another attribute within the
  • function declaration within function declaration with same name javascript
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • Reloading table causes flickering
  • Can you build a truly RESTful service that takes many parameters?
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Many to Many in Linq using Dapper
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • .NET video play library which allows to change the playback rate?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Where can I find tomesh.c?
  • Detecting null parameter in preprocessor macro
  • Android device acting as an accessory
  • xcode don't localize specific strings
  • What and where is mdimport
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • ilmerge with a PFX file
  • Getting last autonumber in access
  • Get object from AWS S3 as a stream
  • How to check if every primary key value is being referenced as foreign key in another table
  • MySQL WHERE-condition in procedure ignored
  • Javascript convert timezone issue
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • RestKit - RKRequestDelegate does not exist
  • C# - Getting references of reference
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • How to get Windows thread pool to call class member function?