Search code examples
mysqldelete-row

MySQL: Delete from database when id first letters are numbers


I am trying to delete a row from my database and the aid which is generated from an external API contains number.

For example this are the aid's:

16RYR3w
15LSPf1
10sLK3
5PlsKs

This is what I'm trying to achieve:

$uid = 1;
$aid = '15LSPf1';

DELETE FROM favorites WHERE uid='". $uid ."' AND aid LIKE '". $aid ."'

This doesn't work. What I'm I doing wrong? I dont have a choice of changing the id because the numbers are in a random order.


Solution

  • This is what you're looking for:

    $aid = stripslashes($_GET['aid']);
    $query="DELETE FROM favorites WHERE uid='". $uid ."' AND aid LIKE '%". $aid ."%'";
    

    And note that LIKE is only 10x faster than REGEXP. More on the test can be found here: http://thingsilearn.wordpress.com/2008/02/28/mysql-query-speed-regexp-vs-like/